问题描述
我使用一种叫做 AL 的语言来工作,但我们有 JSON 对象、数组等。
我正在查询这个 api:http://citibikenyc.com/stations/json 并且我将结果存储在一个文本变量中。
我的目标是将 ID 和站名存储在单独的表中。我不知道应该如何遍历 ID 和 stationName 的所有元素。
我知道路径将是 stationBeanList[0].id 并且每次我不知道如何编写它时都会增加 0。
if Client.Get(url,Response) then begin
if Response.IsSuccessstatusCode then begin
Response.Content.ReadAs(Json);
J.ReadFrom(Json);
JsonBuffer.ReadFromText(Json);
JsonObj.ReadFrom(Json);
// How to iterate though all of the Elements
end;
end;
感谢任何帮助或建议。
解决方法
我想出了以下可行的方法,但请让我知道如何改进。
我讨厌有 resultToken2 和 resulttoken3。
url := 'http://citibikenyc.com/stations/json';
if Client.Get(url,Response) then begin
if Response.IsSuccessStatusCode then begin
Response.Content.ReadAs(Json);
J.ReadFrom(Json);
JsonBuffer.ReadFromText(Json);
if not Jtoken.ReadFrom(Json) then
Error('Invalid response from the web service. Invalid JSON object.');
if not Jtoken.SelectToken('stationBeanList',resultToken) then
Error('Invalid response from the web service. Invalid JSON object.');
if not resultToken.IsArray then
Error('Invalid response from the web service. Invalid JSON object.');
arrayToken := resultToken.AsArray();
for i := 0 to arrayToken.Count() - 1 do begin
arrayToken.get(i,resultToken);
resultToken.AsObject().Get('id',resultToken2);
resultToken.AsObject().Get('stationName',resultToken3);
StationDetails.Init();
StationDetails.ID := resultToken2.AsValue().AsInteger();
StationDetails."Station Name" := resultToken3.AsValue().AsText();
StationDetails.Insert(true);
end;
end;
end;