3

Array:

{ 
    "field':["field1":"value1","field2":"value2"], 
            ["field1":"value1","field2":"value2"]
}

How to parse the above json response in c#

3 Answers 3

7

the json string you provided is not correct in json format, the json array should be:

{"field":[
           {"field1":"value1","field2":"value2"},
           {"field1":"value1","field2":"value2"}
         ]
}

You can use json.net to convert it:

var obj = JsonConvert.DeserializeObject(json);

This tool is also available in nuget.

If you want to use strong type:

public class YourObject
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
}

public class YourClass
{
    public YourObject[] Field { get; set; }
}

var yourClass = JsonConvert.DeserializeObject<YourClass>(json);
Sign up to request clarification or add additional context in comments.

Comments

3

Use newtonsoft json.net for parsing json response.

It is simple and easy

I answered same kind of question here. Take a look at it once

Comments

0

It may be worth taking a look at the javaScriptSerializer Class. and the deserialize method within.

JavaScriptSerializer jss= new JavaScriptSerializer();
User user = jss.Deserialize<User>(jsonResponse); 

2 Comments

Coming back to this question some four years later I would definitely recommend using the other answers if you can include this in your project. In my opinion this answer is only really helpful if you can only use .net framework.
I disagree with the premise of Matt's comment. Anytime you can efficiently operate within the framework your project is built on, you should. Needlessly referencing in out-of-framework libraries can complicate design patterns, mangle conventions, and almost guarantees reliance on scattered documentation. I'd +1 the answer if it provided more detail.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.