SQL Delphi:访问 JSON 数组中的 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9608794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:43:35  来源:igfitidea点击:

Delphi: Accessing JSON Objects within a JSON Array

sqljsondelphidatasnap

提问by bpromas

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.Size 时,结果是 6226004。什么?)

回答by Rob Kennedy

If you have an array from DBXJSON, then it is a TJSONArray. Call its Getmethod 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 forloop:

您还可以使用for循环遍历整个数组:

for Value in jArray do


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

但是,如果您检查Size属性并得到 6226004 而不是 3,则表明这里还有其他问题。我的猜测是,您认为的 aTJSONArray并不是真正的那种类型。用于as进行检查类型转换:

jArray := jPair.JsonValue as TJSONArray;

You'll get an EInvalidCastexception if that fails.

EInvalidCast如果失败,你会得到一个例外。

回答by teran

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

这是一个用于解析和输出 json 数据的示例代码。我已经修改了您的 JSON 数据并添加了ArrayData字段,其中包含您的初始对象数组:

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.