Delphi:在JSON数组中访问JSON对象。

时间:2022-08-26 11:59:23

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

我有一个JSON对象,命名为jObject,看起来是这样的:

{
  "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 amount of objects is dynamic) with multiple "key: values"(these don't vary, being always the same 5 fields), which I want to insert into an SQL database, "key" being column, "value" being field. Question is, how do I access each object individually?

如您所见,它包含两双,其中一个是和三个数组对象在这种情况下(的对象是动态)与多个“关键:价值观”(这些不变化,总是相同的5个字段),我想插入一个SQL数据库,“关键”专栏,“价值”字段。问题是,我如何分别访问每个对象?

Code-wise what I did was extract the pair that contained this array by putting it in jPair

代码方面,我所做的是通过将数组放入jPair中来提取包含这个数组的一对

jPair := OriginalObject.Get(1); 

and then captured the array

然后捕获数组。

jArray:= TJSONArray(jPair.JsonValue);

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

(另外,作为奖励,当我评估jArray时。大小,结果是6226004。什么?)

2 个解决方案

#1


8  

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

如果您有一个来自DBXJSON的数组,那么它就是一个TJSONArray。调用它的Get方法来获取数组的一个元素。

var
  Value: TJSONValue;

Value := jArray.Get(0);

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

你也可以用for循环遍历整个数组:

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:

但是如果你检查Size属性,得到6226004而不是3,这说明这里还有别的问题。我猜你认为的TJSONArray并不是那种类型。用于检查类型转换:

jArray := jPair.JsonValue as TJSONArray;

You'll get an EInvalidCast exception if that fails.

如果失败,您将获得一个EInvalidCast异常。

#2


5  

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:

下面是解析和输出json数据的示例代码。我修改了你的JSON数据,添加了ArrayData字段,wich包含了你的初始对象数组:

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.

#1


8  

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

如果您有一个来自DBXJSON的数组,那么它就是一个TJSONArray。调用它的Get方法来获取数组的一个元素。

var
  Value: TJSONValue;

Value := jArray.Get(0);

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

你也可以用for循环遍历整个数组:

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:

但是如果你检查Size属性,得到6226004而不是3,这说明这里还有别的问题。我猜你认为的TJSONArray并不是那种类型。用于检查类型转换:

jArray := jPair.JsonValue as TJSONArray;

You'll get an EInvalidCast exception if that fails.

如果失败,您将获得一个EInvalidCast异常。

#2


5  

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:

下面是解析和输出json数据的示例代码。我修改了你的JSON数据,添加了ArrayData字段,wich包含了你的初始对象数组:

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.