4

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

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
string json = GetJson(); JArray a = JArray.Parse(json);
Appended the next steps to my answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.