Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse json object in C#?

Array:

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

How to parse the above json response in c#

like image 363
user1667957 Avatar asked Dec 06 '25 08:12

user1667957


2 Answers

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);
like image 98
cuongle Avatar answered Dec 07 '25 20:12

cuongle


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

like image 36
Raghuveer Avatar answered Dec 07 '25 22:12

Raghuveer