XPRIM_RandomAccessJsonReader allows efficient access to JSON values in a JSON string or in HTTP response body. The "random access" in XPRIM_RandomAccessJsonReader reflects the reader's nature where you can get the value of any element located anywhere in the JSON string, at any time. You don't have to read the JSON values sequentially, which is the case with XPRIM_JsonReader (will be discussed next).
Start by creating an XPRIM_RandomAccessJsonReader object.
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader)
Use one of the following methods to set the source of the reader:
We'll use the same JSON string that we used in the previous section as an example.
{
"name":
{
"given": "John",
"surname": "Smith"
},
"age": 45,
"contactNo":
[
{ area: "02", no: "9378 2867", type: "landline" },
{ no: "0468 732 371", type: "mobile" }
]
}
Let's assume that this is a response from a HTTP request called #Request, use the SetSourceHttpResponse to read from the response body.
#Reader.SetSourceHttpResponse HttpResponse(#Request.Response)
There are three sets of Read… methods you can use to extract the values from your JSON:
Each set contains the following variants:
There are also 2 set of Begin… methods that you can use to "enter" an object or array:
The Begin… methods navigates into the specified element. To exit and return to the parent element, use the matching End… methods (EndObject and EndArray).
All Read…/Begin… methods has a Found output Boolean parameter that indicates whether it successfully retrieves the value or not.
Next: Read…WithPath