0

I work in a language called AL for work but we have JSON Objects, Arrays, etc..

I'm querying this api: http://citibikenyc.com/stations/json and I have the result stored in a text variable.

My objective is to store the ID and Station Names in a separate table. I can't figure out how I should iterate through all of the elements for ID and stationName.

I understand the path will be stationBeanList[0].id and increment the 0 every time I just don't know how to write it.

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;

Any help or suggestions is appreciated.

1 Answers1

0

I came up with the following which works but please lot me know how it could be improved.

I hate having a resultToken2 and a 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;