我正在使用API这样输出JSON文件,因此无法编辑JSON文件的格式。有什么方法可以遍历每个盘中日期,以找到每个日期中open的值?
我尝试使用Object.intraday[0].open
,但这似乎不起作用,因为它不包含在数组中?
我意识到我可以使用Object.intraday.date.open
(日期为例如“ 2018-10-19 15:59:00:”);但是我希望能够索引到不同的时间。
{
"symbol": "AAPL",
"stock_exchange_short": "NASDAQ",
"timezone_name": "America/New_York",
"intraday": {
"2018-10-19 15:59:00:" {
"open": "219.49",
"close": "219.23",
"high": "219.61",
"low": "219.19",
"volume": "302415"
},
"2018-10-19 15:58:00:" {
"open": "219.62",
"close": "219.48",
"high": "219.70",
"low": "219.48",
"volume": "173762"
},
....
这是我使用的Pascal代码,它使用{"intraday":{"2018-10-1915:59:00":{"open":"23","low":"4"},"2018-10-1915:58:00":{"open":"25","low":"21"}}}
的测试JSON
JSONValue := TJSONObject.ParseJSONValue('{"intraday":{"2018-10-1915:59:00":{"open":"23","low":"4"},"2018-10-1915:58:00":{"open":"25","low":"21"}}}');
j:=0;
begin
if JSONVAlue is TJSONObject then
begin
// Get the quote and low values
quote := JSONValue.GetValue<string>('intraday[0]');
low := JSONValue.GetValue<string>('intraday.low');
Memo1.Lines.Add(quote + ': ' + low);
j := j+1;
end;
end;
答案 0 :(得分:3)
procedure TfmMain.ParseIntraDay(AMemo: TMemo);
var
LObject, LIntraDayObj: TJSONObject;
LEnumerator: TJsonPairEnumerator;
LQuote, LLow: String;
begin
LObject := TJSONObject.ParseJSONValue('{"intraday":{"2018-10-19 15:59:00":{"open":"23","low":"4"},"2018-10-19 15:58:00":{"open":"25","low":"21"}}}') as TJSONObject;
try
LIntraDayObj := LObject.GetValue('intraday') as TJSONObject; //{"2018-10-19 15:59:00":{"open":"23","low":"4"},"2018-10-19 15:58:00":{"open":"25","low":"21"}}
LEnumerator := LIntraDayObj.GetEnumerator;
while LEnumerator.MoveNext do
begin
LQuote := LEnumerator.Current.JsonString.Value;
LLow := (LEnumerator.Current.JsonValue As TJsonObject).Values['low'].Value;
AMemo.Lines.Add(String.Format('%s: %s', [LQuote, LLow]));
end;
finally
LObject.Free;
end;
end;
应在备忘录中提供以下输出:
2018-10-19 15:59:00: 4
2018-10-19 15:58:00: 21
您可以将其用作所需内容的基础。