Delphi: Accessing JSON Objects within a JSON Array

前端 未结 2 982
醉梦人生
醉梦人生 2021-01-02 11:15

I have a JSON Object, let\'s name it jObject that looks like this:

{
  \"id\": 0,
  \"data\": \"[{DAT_INCL: \\\"08/03/2012 10:07:08\\\", NUM_ORDE: 1, NUM_ATN         


        
相关标签:
2条回答
  • 2021-01-02 11:52

    If you have an array from DBXJSON, then it is a TJSONArray. Call its Get method to get an element of the array.

    var
      Value: TJSONValue;
    
    Value := jArray.Get(0);
    

    You can also go through the entire array with a for loop:

    for Value in jArray do
    

    But if you check the Size property and get 6226004 instead of 3, that suggests there's something else wrong here. My guess is that what you think is a TJSONArray isn't really that type. Use as to do a checked type cast:

    jArray := jPair.JsonValue as TJSONArray;
    

    You'll get an EInvalidCast exception if that fails.

    0 讨论(0)
  • 2021-01-02 12:15

    here is an sample code to parse and output your json data. I've modified your JSON data and added ArrayData field, wich contains your initial array of objects:

    program Project1;
    {$APPTYPE CONSOLE}
    {$R *.res}
    
    uses
      System.SysUtils, dbxjson;
    
    const JSON_DATA = '{"ArrayData":['+
                        '{"DAT_INCL":"07/03/2012 17:33:03", "NUM_ORDE":1,"NUM_ATND":1, "NUM_ACAO":2, "NUM_RESU":3},'+
                        '{"DAT_INCL":"07/03/2012 17:33:05", "NUM_ORDE":2,"NUM_ATND":1, "NUM_ACAO":4, "NUM_RESU":5},'+
                        '{"DAT_INCL":"07/03/2012 17:33:05", "NUM_ORDE":3,"NUM_ATND":1, "NUM_ACAO":8, "NUM_RESU":null}'+
                       ']}';
    
    
    var jsv   : TJsonValue;
        originalObject : TJsonObject;
    
        jsPair : TJsonPair;
        jsArr : TJsonArray;
        jso  : TJsonObject;
        i : integer;
    begin
        try
            //parse json string
            jsv := TJSONObject.ParseJSONValue(JSON_DATA);
            try
                //value as object
                originalObject := jsv as TJsonObject;
    
                //get pair, wich contains Array of objects
                jspair := originalObject.Get('ArrayData');
                //pair value as array
                jsArr := jsPair.jsonValue as  TJsonArray;
    
                writeln('array size: ', jsArr.Size);
                //enumerate objects in array
                for i := 0 to jsArr.Size - 1 do begin
                    writeln('element ', i);
                    // i-th object
                    jso := jsArr.Get(i) as TJsonObject;
    
                    //enumerate object fields
                    for jsPair in jso do begin
                        writeln('   ', jsPair.JsonString.Value, ': ', jsPair.JsonValue.Value);
                    end;
                end;
            finally
                jsv.Free();
                readln;
            end;
        except
            on E: Exception do
              Writeln(E.ClassName, ': ', E.Message);
        end;
    end.
    
    0 讨论(0)
提交回复
热议问题