Akshay chavda
Akshay chavda

Reputation: 39

How can i access all variable values in json list

I want to access variables values from json list in C#. my json String is as below

plase suggest me the way to access variable from json list .

example :-

 {
                  "BillTransList": [
                    {
                      "aa": "13",
                      "ss": "200",
                      "LessItemList": [
                        {
                          "a": "13",
                          "b": "19"
                        },
                        {
                          "a": "17",
                          "b": "18"
                        }
                      ]
                    },
                    {
                      "aa": "13",
                      "ss": "200",
                      "LessItemList": [
                        {
                          "a": "3",
                          "b": "9"
                        },
                        {
                          "a": "7",
                          "b": "8"
                        }
                      ]
                    }
                  ],
                  "aq": "2"
                }

suggestions and corrections are welcome

thanks

Upvotes: 2

Views: 352

Answers (2)

Chris Shao
Chris Shao

Reputation: 8231

Aliasgar Rajpiplawala's answer is Nice.

And I will do some supplement:

You should use some library to deserialize JSON string, such like Json.NET (Newtonsoft JSON).

And you can create your class using Json2Csharp (http://json2csharp.com/). Paste your json into it, and your class will be generated automic.

Upvotes: 0

Aliasgar Rajpiplawala
Aliasgar Rajpiplawala

Reputation: 656

You can create class to deserialize your JSON.

public class LessItemList
{
    public string a { get; set; }
    public string b { get; set; }
}

public class BillTransList
{
    public string aa { get; set; }
    public string ss { get; set; }
    public List<LessItemList> LessItemList { get; set; }
}

public class RootObject
{
    public List<BillTransList> BillTransList { get; set; }
    public string aq { get; set; }
}

than you can deserialize using the below code

var jsonResponse = JsonConvert.DeserializeObject<RootObject>(result);

than you can access LessItemList simply by using LINQ.

Upvotes: 2

Related Questions