Page 1 of 1
#XPRIM_Json Question
Posted: Fri Aug 25, 2017 4:50 pm
by soa
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",
}
]
Re: #XPRIM_Json Question
Posted: Mon Aug 28, 2017 9:22 am
by tsupartono
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.
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
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.
Re: #XPRIM_Json Question
Posted: Mon Aug 28, 2017 10:17 am
by soa
Thanks Tony, I'll give that a go.
Re: #XPRIM_Json Question
Posted: Sat Feb 10, 2018 4:45 pm
by soa
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
Re: #XPRIM_Json Question
Posted: Mon Feb 12, 2018 11:03 am
by tsupartono
HI Jim,
Please see below a
JsonReader example of parsing a JSON array containing objects like so:
Code: Select all
[
{"value": 1},
{"value": 2}
. . .
]
The example below uses
SetSourceString to set the source string. Replace that with
SetSourceHttpResponse if you are parsing a HTTP response data.
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
Posted: Mon Feb 12, 2018 2:27 pm
by soa
Thanks, Tony I'll give that a go.