【问题标题】:Understanding JSON Iteration了解 JSON 迭代
【发布时间】:2021-04-01 12:28:02
【问题描述】:

我使用一种称为 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;

感谢任何帮助或建议。

【问题讨论】:

    标签: json dynamics-al


    【解决方案1】:

    我想出了以下可行的方法,但请告诉我如何改进它。

    我讨厌有一个 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;
    

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 2016-04-18
      • 2014-01-20
      • 2011-08-02
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多