3

Array:

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

How to parse the above json response in c#

cuongle
  • 74,024
  • 28
  • 151
  • 206
user1667957
  • 81
  • 1
  • 1
  • 10

3 Answers3

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);
Sabuncu
  • 5,095
  • 5
  • 55
  • 89
cuongle
  • 74,024
  • 28
  • 151
  • 206
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

Community
  • 1
  • 1
Raghuveer
  • 2,630
  • 3
  • 29
  • 59
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); 
Matt Skeldon
  • 577
  • 8
  • 23
  • 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. – Matt Skeldon May 13 '16 at 17:33
  • 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. – LanchPad Aug 31 '18 at 13:29