嵌套 JSON 个对象
Nested JSON Objects
我正在使用 API 输出 JSON 文件,因此无法编辑 JSON 文件的格式。有什么方法可以遍历每个日内日期,找到每个日期的开盘价吗?
我试过使用 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;
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
您可以将此作为您想要的基础。
我正在使用 API 输出 JSON 文件,因此无法编辑 JSON 文件的格式。有什么方法可以遍历每个日内日期,找到每个日期的开盘价吗?
我试过使用 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"}}}
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;
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
您可以将此作为您想要的基础。