Delphi:访问JSON数组中的JSON对象 [英] Delphi: Accessing JSON Objects within a JSON Array
问题描述
我有一个JSON对象,让我们将它命名为jObject,如下所示:
{
/ pre>
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}]
}
如您所见,它包含两对,其中一个是在这种情况下有三个对象的数组(对象的数量是动态的),多个键:值(这些不变,总是相同的5个字段),我想插入到SQL数据库中,key是列,value是字段。问题是,我如何单独访问每个对象?
代码我做的是通过将它放在jPair中来提取包含该数组的对
jPair:= OriginalObject.Get(1);
然后捕获数组
jArray:= TJSONArray(jPair.JsonValue);
(另外,作为一个奖励,当我评估jArray.Size时,结果是6226004.什么? )
解决方案如果您有一个数组来自DBXJSON,那么它是一个
TJSONArray
。调用其Get
方法来获取数组的元素。var
值:TJSONValue;
值:= jArray.Get(0);
您还可以通过 c
$ c> loop: for jArray do
但是,如果您查看
Size
属性并获取6226004而不是3这表明这里还有其他的错误。我的猜测是,你认为是一个TJSONArray
并不是那种类型。使用作为 c $ c来执行选中的类型转换:
jArray:= jPair .JsonValue为TJSONArray;
你会得到一个
EInvalidCast
异常if失败了。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_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?
Code-wise what I did was extract the pair that contained this array by putting it in 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?)
解决方案If you have an array from DBXJSON, then it is a
TJSONArray
. Call itsGet
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 aTJSONArray
isn't really that type. Useas
to do a checked type cast:jArray := jPair.JsonValue as TJSONArray;
You'll get an
EInvalidCast
exception if that fails.这篇关于Delphi:访问JSON数组中的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!