Parse JSON in .NET runtime
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...
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.
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
精彩评论