JSON.Net

M

mick

Recently started learning Java and android programming and used, for the first
time JSON (in c# I always used XML for web responses). Anyway I thought I'd have
a try to parse JSON in C#. I read about a bit and people seem to be using
JSON.Net so I thought I'd give it a go. Oh dear. The author seems to have chosen
to take a minimalist approach to the docs Plenty of them, just not very
detailed. I've wasted way too many hours trying to work out how to do it but am
no further forward.

Here is the address for a site (which gives cricket scores)
http://pipes.yahoo.com/pipes/pipe.run?_id=tMcVGcqn3BGvsT__2R2EvQ&_render=json

Here is a JSON formatter to make it clear whats going on.
http://jsonformatter.curiousconcept.com/

What I can't fathom is how to get to the "items" array using JSON.net. In
Java/Android it's
easy-
JSONObject result... // (is the JSON response)
JSONObject value= result.getJSONObject("value");
JSONArray array = value.getJSONArray("items");


Here's the doc on json.net array parsing which doesn't help if the array is
nested.
http://james.newtonking.com/json/help/index.html?topic=html/ParseJsonArray.htm

Anyone with any experience with this library give me a clue? Or does anyone know
of a better, less clunky library?
I would have been better off parsing the string myself I think :)

mick
 
A

Arne Vajhøj

Recently started learning Java and android programming and used, for the
first time JSON (in c# I always used XML for web responses). Anyway I
thought I'd have a try to parse JSON in C#. I read about a bit and
people seem to be using JSON.Net so I thought I'd give it a go. Oh dear.
The author seems to have chosen to take a minimalist approach to the
docs Plenty of them, just not very detailed. I've wasted way too many
hours trying to work out how to do it but am no further forward.

Here is the address for a site (which gives cricket scores)
http://pipes.yahoo.com/pipes/pipe.run?_id=tMcVGcqn3BGvsT__2R2EvQ&_render=json


Here is a JSON formatter to make it clear whats going on.
http://jsonformatter.curiousconcept.com/

What I can't fathom is how to get to the "items" array using JSON.net.
In Java/Android it's
easy-
JSONObject result... // (is the JSON response)
JSONObject value= result.getJSONObject("value");
JSONArray array = value.getJSONArray("items");


Here's the doc on json.net array parsing which doesn't help if the array
is nested.
http://james.newtonking.com/json/help/index.html?topic=html/ParseJsonArray.htm


Anyone with any experience with this library give me a clue? Or does
anyone know of a better, less clunky library?
I would have been better off parsing the string myself I think :)

No need for a third party library.

..NET comes with JSON support.

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

Arne
 
A

Anders Eriksson

Recently started learning Java and android programming and used, for the
first time JSON (in c# I always used XML for web responses). Anyway I
thought I'd have a try to parse JSON in C#. I read about a bit and
people seem to be using JSON.Net so I thought I'd give it a go. Oh dear.
The author seems to have chosen to take a minimalist approach to the
docs Plenty of them, just not very detailed. I've wasted way too many
hours trying to work out how to do it but am no further forward.

Here is the address for a site (which gives cricket scores)
http://pipes.yahoo.com/pipes/pipe.run?_id=tMcVGcqn3BGvsT__2R2EvQ&_render=json

One way is to use Json2Csharp to create classes for the JSon data.
http://json2csharp.com/

It will create a number of classes (in this case 4). You then just use
the RootObject class like this

RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);

and to get all items

List<Item> items = obj.value.items;


Disclaimer: I'm really a beginner!

// Anders
 
M

mick

One way is to use Json2Csharp to create classes for the JSon data.
/http://json2csharp.com/

I did actually look at this but because it created more that one class
I didnt know which to use in the Deserialise bit. Also it was generating
some '__invalid_name' fields and I don't know what to do about.
It will create a number of classes (in this case 4). You then just use the
RootObject class like this

RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);

and to get all items
List<Item> items = obj.value.items;

That's the way I was hoping it would work in the first place.

Turns out the way you do it in Json.net is something along the lines of
JObject jobject = JObject.Parse(s);
var x = jobject["value"];
var array = x["items"];
Disclaimer: I'm really a beginner!
I can only hope to reach those heights!

mick
 
M

mick


Yes I know. I was hoping for something a lot simpler to get me started with
this. The example there
seems to create the json from objects that the user created in the first place.
That would be easy
to go back and forth. Mapping someone elses json to classes is something I'm a
lot less sure of.
jsontocsharpe that Anders mentioned would seem to be ideal if I knew what to do
about the
__invalid_name fields.

While I think of it, if I created a class that maps to the json do the property
names have to match the field names in the json? Do I have to map to *all*
of the fields in the json?


mick
 
A

Anders Eriksson

I did actually look at this but because it created more that one class
I didnt know which to use in the Deserialise bit. Also it was generating
some '__invalid_name' fields and I don't know what to do about.

It is using the field name in the json for creating the variable name
and colon is not a valid variable name.

Just replace : with _

public YId __invalid_name__y:id { get; set; }
public string __invalid_name__y:title { get; set; }
public string __invalid_name__y:repeatcount { get; set; }

to

public YId y_id { get; set; }
public string y_title { get; set; }
public string y_repeatcount { get; set; }

and it should work.

// Anders
 
M

mick

It is using the field name in the json for creating the variable name
and colon is not a valid variable name.

Just replace : with _

public YId __invalid_name__y:id { get; set; }
public string __invalid_name__y:title { get; set; }
public string __invalid_name__y:repeatcount { get; set; }

to

public YId y_id { get; set; }
public string y_title { get; set; }
public string y_repeatcount { get; set; }

and it should work.

Wow, cheers mate. It worked like a charm. Now that I look closer at the
generated classes I see that the root embraces them all:)

Thanks again,
mick
 
Top