Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON in .NET runtime

Tags:

json

c#

.net

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...

like image 722
Akim Khalilov Avatar asked Dec 09 '22 17:12

Akim Khalilov


1 Answers

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.

like image 181
Lodewijk Avatar answered Dec 25 '22 13:12

Lodewijk