Delphi: accessing JSON objects in a JSON array

I have a JSON object, name it jObject, which looks like this:

{ "id": 0, "data": "[{DAT_INCL: \"08/03/2012 10:07:08\", NUM_ORDE: 1, NUM_ATND: 1, NUM_ACAO: 2, NUM_RESU: 3}, {DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 2, NUM_ATND: 1, NUM_ACAO: 4, NUM_RESU: 5}, {DAT_INCL: \"08/03/2012 10:07:09\", NUM_ORDE: 3, NUM_ATND: 1, NUM_ACAO: 8, NUM_RESU: NULL}]" } 

As you can see, it contains two pairs, one of which is an array with three objects in this case (the number of objects is dynamic) with several "keys: values" (they do not change, they are always the same 5 fields) that I want insert into the SQL database, "key" is a column, "value" is a field. The question is, how can I access each object individually?

In the code that I did, the pair containing this array was extracted, putting it in jPair

 jPair := OriginalObject.Get(1); 

and then captured an array

 jArray:= TJSONArray(jPair.JsonValue); 

(Also, as a bonus, when I evaluate jArray.Size, the result is 6226004. What?)

+6
json sql delphi datasnap
source share
2 answers

If you have an array from DBXJSON, then this is TJSONArray . Call its Get method to get an array element.

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

You can also traverse the entire array using a for loop:

 for Value in jArray do 

But if you check the Size property and get 6226004 instead of 3, it means that something is wrong here. I assume that what you consider TJSONArray is not really like that. Use as to execute a validated type:

 jArray := jPair.JsonValue as TJSONArray; 

You will get an EInvalidCast exception if this fails.

+8
source share

here is sample code for parsing and outputting json data. I changed the JSON data and added an ArrayData field that contains your original 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. 
+5
source share

All Articles