tags:

views:

275

answers:

2

Hi. I want to get some response from WebServer.

The returning data looks like this:

[[3014887,"string1 string","http://num60.webservice.com/u3014887/b_c9c0625b.jpg",0], [3061529,"string2 string","http://num879.webservice.com/u3061529/b_320d6d36.jpg",0], [7317649,"string3 string","http://num1233.webservice.com/u7317649/b_a60b3dc2.jpg",0], [12851194,"string4 string","http://num843.webservice.com/u12851194/b_4e273fa4.jpg",0], [15819606,"string5 string","http://num9782.webservice.com/u15819606/b_66333a8f.jpg",0], [15947248,"string6 string","http://num1500.webservice.com/u15947248/b_920c8b64.jpg",0]]

I think is in the JSON format, but I couldn't parse it in my .Net WinForm application. Can you provide some advise or exampe how to do that.

I googled about JSON.NET library, DataContractJsonSerializer class, but I couldn't understand how to glue it all together with the response's data type...

+6  A: 

If you want to parse JSON, then the JSON.net library is the place to be.

You can use it like this:

var json = @"[[3014887,""string1 string"",""http://num60.webservice.com/u3014887/b_c9c0625b.jpg"",0], 
                      [3061529,""string2 string"",""http://num879.webservice.com/u3061529/b_320d6d36.jpg"",0],
                      [7317649,""string3 string"",""http://num1233.webservice.com/u7317649/b_a60b3dc2.jpg"",0],
                      [12851194,""string4 string"",""http://num843.webservice.com/u12851194/b_4e273fa4.jpg"",0],
                      [15819606,""string5 string"",""http://num9782.webservice.com/u15819606/b_66333a8f.jpg"",0], 
                      [15947248,""string6 string"",""http://num1500.webservice.com/u15947248/b_920c8b64.jpg"",0]]";
var array = JArray.Parse(json);

foreach (var token in array)
{
    Console.WriteLine(token[0]);
}

This way I could read the contents of your array.

Lodewijk
I have a square brackets. Also there're an array of the values, not KeyValue pairs in my example. It's a problem to use JSON.NET with my example, is it?
Akim Khalilov
string json = GetJson();JArray a = JArray.Parse(json);
James Newton-King
And what next steps?
Akim Khalilov
Appended the next steps to my answer.
Lodewijk
A: 

There is JSON(De)serialization in the WCF namespace (Windows Communication Support for AJAX Integration and JSON ) there is also the very popular (more powerful) JSON (de)serialization library JSON.Net

flq