izeko
izeko

Reputation: 175

read json string

Can anyone help me read the following json string into objects.

[ { "category": "burglary", "month": "2011-08", "location": { "latitude": "52.6361452", "street": { "id": 62819, "name": "On or near Burton Street" }, "longitude": "-1.1243242" }, "context": "", "id": 4051044 },...

I have created a class containing objects of each type called 'crimes' and then something like the following:

Dim listArray As New List(Of crimes)()
Using jsonStream = New System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(strString))
    Dim serializer As New DataContractJsonSerializer(GetType(List(Of crimes)))
    Dim array As List(Of crimes) = TryCast(serializer.ReadObject(jsonStream), List(Of crimes))
    listArray = array
End Using

but this gives me a security exception.

Any help apreciated or a nudge in the right direction would be helpful.

Upvotes: 1

Views: 283

Answers (2)

Hot Licks
Hot Licks

Reputation: 47739

Basically, JSON reads the same regardless of the computing language you're using. The difference is in the objects it creates.

There will be basically five:

  1. Array
  2. Dictionary (what JSON calls an "object")
  3. String
  4. Number (with variations -- float, int, etc)
  5. Something to represent "NULL"

Your string starts with a [, which represents an array. In Objective-C this would come back as an NSArray, in Java maybe an ArrayList. Not sure what's typical for C#.

So inside the array are elements, separated by commas (,). These can, in the general case, be any of the above 5 types. The next symbol is {, representing a dictionary/object. This is a collection of name/value pairs. In Objective-C it would be a NSDictionary, in Java perhaps a HashMap. Then you see the name/value pairs, the two parts of each pair separated by :, and pairs separated from each other by ,. The first part is the name, always in quotes and always a character string. The second part is the value, which can be any of the above 5 types. (Keep in mind that array elements are ordered, but the name/value pairs in an "object" can be in any order -- order is not preserved when the JSON is converted from one form to another.)

In your case the first name/value pair is "category": "burglary". This means that the item named "category" has a value of "burglary".

A little farther in you see "location": { "latitude": "52.6361452" .... This means that the name/value pair has a name of "location", and a value that is another dictionary/object. So when you fetch the value "location" from the output of your parser you'll get back your language's representation of a dictionary/object.

Note that this implies that, for the general case, you need to check the type of object you get back as you "peel the layers of the onion" to get to the data item you want.

But some JSON toolkits provide a path-based mechanism to fetch items you want. Eg, ask for "0.location.latitude" or some such and get "52.6361452" as the result.

Upvotes: 0

markS
markS

Reputation: 580

my suggestion is to use Json.Net, you can write the code to manually do the parsing, but that's what libraries are for :) Json.NET

Upvotes: 1

Related Questions