none
How to read Json data using List<T> in c# RRS feed

  • Question

  • Dear all,

    I've the following Json format and would like to read items under menu using List<T> in c#. I am able to read menu but not able to find a way to read items. Please share your ideas. I appreciate your help and thank you very much!

    I am using the following code and not able to declare the datatype to store items collection in the APIData class (I created), declared string data type in the APIData class to read menu:

    ==========================================================================

    HttpResponseMessage responseMessage = client.GetAsync(url).Result;
    if (responseMessage.IsSuccessStatusCode)
    {
    var responseData = responseMessage.Content.ReadAsStringAsync().Result;
    var list = JsonConvert.DeserializeObject<List<APIData>>(responseData);
    foreach (var data in list)
    {

    string menu = data.menu;
     }

    }

    ======================================================================================================

    public class APIData
    {
    public string menu { get; set; }

    }

    ==========================================================================

    Json Format:

    {
        "$id": "1",
        "Id": "99a08d4b-8e20-4811-beee-1b56ac545f90",
        "menu": "Menu 04",
        "items": [
          {
            "$id": "2",
            "Id": "90ed4d57-7921-4c66-b208-4e312a9852e6",
            "Name": "Paprika Sausage",
            "Length": 6,
            "Width": 3,
            "Duration": "00:08:00",
            "Quantity": 40
          },
          {
            "$id": "3",
            "Id": "47614f4d-2621-40de-8be7-e35abed8ed44",
            "Name": "Veal",
            "Length": 8,
            "Width": 4,
            "Duration": "00:08:00",
            "Quantity": 10
          }
        ]
      }
    ]

    ===============================================================


    Narayana Reddy G

    Monday, January 7, 2019 7:53 AM

Answers

  • You can use visual studio special copy json to c# class or json2csharp.com to get the class represenation for your json format  which is n generated as following:

    public class Item
    {
        public string __invalid_name__$id { get; set; }
        public string Id { get; set; }
        public string Name { get; set; }
        public int Length { get; set; }
        public int Width { get; set; }
        public string Duration { get; set; }
        public int Quantity { get; set; }
    }
    
    public class ApiData
    {
        public string __invalid_name__$id { get; set; }
        public string Id { get; set; }
        public string menu { get; set; }
        public List<Item> items { get; set; }
    }

    and then you can deserialize :

    var result = JsonConvert.DeserializeObject<ApiData>(responseData);


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 7, 2019 8:21 AM

All replies

  • You can use visual studio special copy json to c# class or json2csharp.com to get the class represenation for your json format  which is n generated as following:

    public class Item
    {
        public string __invalid_name__$id { get; set; }
        public string Id { get; set; }
        public string Name { get; set; }
        public int Length { get; set; }
        public int Width { get; set; }
        public string Duration { get; set; }
        public int Quantity { get; set; }
    }
    
    public class ApiData
    {
        public string __invalid_name__$id { get; set; }
        public string Id { get; set; }
        public string menu { get; set; }
        public List<Item> items { get; set; }
    }

    and then you can deserialize :

    var result = JsonConvert.DeserializeObject<ApiData>(responseData);


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 7, 2019 8:21 AM
  • Thanks a lot for your quick reply, very much appreciated. I will try it and let you know.

    Narayana Reddy G

    Monday, January 7, 2019 9:09 AM
  • You may try this code o read JSON code:

    void Main() { string json = "{\"People\":[{\"FirstName\":\"Hans\",\"LastName\":\"Olo\"},{\"FirstName\":\"Jimmy\",\"LastName\":\"Crackedcorn\"}]}"; var result = JsonConvert.DeserializeObject<RootObject>(json); var firstNames = result.People.Select (p => p.FirstName).ToList(); var lastNames = result.People.Select (p => p.LastName).ToList(); } public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public class RootObject { public List<Person> People { get; set; } }


    Monday, January 7, 2019 9:21 AM
  • seems a good solution
    Monday, January 7, 2019 3:50 PM
  • Thank you very much, Ehsan. It is working good.

    Narayana Reddy G

    Tuesday, January 8, 2019 9:43 AM
  • Thank you very much Shruthi for sharing this information. I have not tried it yet as the first replied solution worked for me. I appreciate your help.

    Narayana Reddy G

    Tuesday, January 8, 2019 9:47 AM