I am using a json service which returns an array (see below). I don't know how many entries are in the array.
How do I iterate through these elements and extract the data?
[
{
"id":1,
"title":"numeracy",
"version":"1",
},
{
"id":3,
"title":"Minimum Standards Numeracy",
"version":"17",
},
{
"id":4,
"title":"Minimum Standards Writing (Group A)",
"version":"19",
},
{
"id":5,
"title":"Minimum Standards Reading",
"version":"20",
},
{
"id":6,
"title":"Minimum Standards Writing (Group B)",
"version":"21",
},
{
"id":7,
"title":"Minimum Standards Writing (Group C)",
"version":"22",
}
]
#XPRIM_Json Question
-
tsupartono
Re: #XPRIM_Json Question
Jim, please find an example below how to iterate through an array element.
The code assumes the RootArray points to your root array, and that each element in the array is an object.
Keep in mind that there is an EPC coming up soon for 14 SP1 that includes a new JsonReader component, which is designed to read big JSON objects more efficiently. It is not going to make much difference if you have only a few items in your JSON, but it definitely will if you have 100s or more.
The code assumes the RootArray points to your root array, and that each element in the array is an object.
Code: Select all
Define_Com Class(#STD_INT) Name(#i)
Define_Com Class(#XPRIM_JsonObject) Name(#Item) Reference(*DYNAMIC)
Begin_Loop Using(#i) To(#RootArray.ItemCount)
#Item <= #RootArray.ItemAt<#i> *As #XPRIM_JsonObject
End_Loop
Re: #XPRIM_Json Question
Thanks Tony, I'll give that a go.
Re: #XPRIM_Json Question
I see now that the JsonReader component but the documentation stops at RandomAccessJsonreader. I'm after an example of reading through a large array.
Cheers
Jim
Cheers
Jim
-
tsupartono
Re: #XPRIM_Json Question
HI Jim,
Please see below a JsonReader example of parsing a JSON array containing objects like so:
The example below uses SetSourceString to set the source string. Replace that with SetSourceHttpResponse if you are parsing a HTTP response data.
Please see below a JsonReader example of parsing a JSON array containing objects like so:
Code: Select all
[
{"value": 1},
{"value": 2}
. . .
]
Code: Select all
Define_Com Class(#XPRIM_JsonReader) Name(#Reader)
Define_Com Class(#PRIM_BOLN) Name(#OK)
Define_Com Class(#XPRIM_JsonObject) Name(#JsonObj) Reference(*DYNAMIC)
#Reader.SetSourceString String('[{"value": 1}, {"value": 2}]')
#Reader.ReadBeginArray Found(#OK)
#Reader.MoveNext Ok(#OK)
Dowhile Cond(#OK *And (*Not #Reader.IsEndArray))
#Reader.ReadObject Found(#OK) Result(#JsonObj)
#Reader.MoveNext Ok(#OK)
#STD_NUM := #JsonObj.GetNumber('value')
Endwhile
If (*Not #OK)
* Something is wrong.....
Endif
Re: #XPRIM_Json Question
Thanks, Tony I'll give that a go.