JSON Parser


Parsing JSON with VDF

Much like when parsing raw JSON in other frameworks like Cocoa or .NET, my JSON parser returns a dictionary (cJSONDictionary). Since JSON lacks schemas it's up to the programmer to convert the data to a more easy-to-use structure. I simply provide a neat way of working with the data. (Remember to check out the JSON Struct Generator).

cJSONParser basics

Dependencies

Using the parser

The cJSONParser class is disposable after use. It's sole purpose is parsing a string containing serialized JSON to a cJSONDictionary with the Parse method. The cJSONDictionary is a subclass of cHashTable and values for JSON members are fetched via Get Value of "member" to myVariable. Member existence checks are done with the (found) indicator or checking against NULL_ENTRY.

Helpful tip

To watch/debug the members of a cJSONDictionary you can do this:

Data mapping

JSON element type VDF data type
object cJSONDictionary instance (handle)
array (including array of objects) variant[]
value variant

Code example

sJSON =


Download

Share

6 thoughts on JSON Parser

  1. Pingback: The JSON Parser | { eriksven.com }

  2. Hi Erik,

    Thanks for sharing this pkg! Been looking for a VDF JSON parser – this work very well!

    But I think I found a small bug in the parser, I used http://maps.googleapis.com/maps/api/geocode/json?address=new%20zealand&sensor=true to return the following JSON string:

    {
    “results” : [
    {
    “address_components” : [
    {
    “long_name” : “New Zealand”,
    “short_name” : “NZ”,
    “types” : [ “country”, “political” ]
    }
    ],
    “formatted_address” : “New Zealand”,
    “geometry” : {
    “bounds” : {
    “northeast” : {
    “lat” : -29.2313419,
    “lng” : -175.831536
    },
    “southwest” : {
    “lat” : -52.61941849999999,
    “lng” : 165.8694369
    }
    },
    “location” : {
    “lat” : -40.900557,
    “lng” : 174.885971
    },
    “location_type” : “APPROXIMATE”,
    “viewport” : {
    “northeast” : {
    “lat” : -34.1295578,
    “lng” : 179.0625357
    },
    “southwest” : {
    “lat” : -52.61941849999999,
    “lng” : 165.8700053
    }
    }
    },
    “types” : [ “country”, “political” ]
    }
    ],
    “status” : “OK”
    }

    On trying to parse and return the following data results->geometry->location->lat

    The value returned was 40.900557 instead of -40.900557

    Seems to be missing the “-” symbol.

    Thanks.

    • If you don’t have a nested structure, you can simply access the member properties like this:

  3. We use your JSON parser for a lot of hosted site API syncing with our DF app, a CRM for non-profits.

    Some of our parsing logic uses “Value” and other lines use ValueAtPath.

    We seem to get results using both – but could you provide an example of when ValueAtPath should be used?

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *