Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 5

...

To efficiently read a JSON response, use either XPRIM_RandomAccessJsonReader or XPRIM_JsonReader.

Info

XPRIM_RandomAccessJsonReader allows you to read any part of your JSON data, which you can refer to by name or by index. XPRIM_JsonReader is a fast, forward-only reader. It reads the JSON data the way it reads data on a tape. Your JSON data is read sequentially, so you can't directly access different areas of your JSON data. XPRIM_JsonReader should be used when reading huge JSON data (such as those returned by database services), as it consumes very little memory (whereas XPRIM_RandomAccessJsonReader use of memory is linear to the size of the JSON data).

Most of the time, you would choose XPRIM_RandomAccessJsonReader over XPRIM_JsonReader as it is much easier to use. Unless your JSON data is really big, always opt for XPRIM_RandomAccessJsonReader.

Example - Reading the Response Body as JSON

...

We are interested in the lat and long values, which can be accessed via results (element 1) >> geometry >> location.

     Define_Com Class(#XPRIM_HttpRequest) Name(#Req)
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader)
...
#Req.DoGet Url('https://maps.googleapis.com/maps/api/geocode/json?...')

* Check if request is successful

     If (#Req.Response.IsSuccessHttpStatusCode)

    * Set the JSON reader source fot response from the HTTP request   

       #Reader.SetSourceHttpResponse HttpResponse(#Req.Response)

    * Navigate to the 'location' object (containing the 'lat' and 'lng' values)

...

    * Names and indexes in a path are separated by forward slashes   

       #Reader.BeginObjectWithPath Path('results/1/geometry/location')

    * Get the latitude and longitude value

      

...

 #Latitude := #Reader.ReadNumberWithName('lat')
 

...

 #Longitude := #Reader.ReadNumberWithName('lng')

    * Close "BeginObject" with "EndObject"

      

...

 #Reader.EndObject
Endif

Next: Checking for Invalid Responses