Search code examples
c#jsonarraylistjson-deserialization

Deserializing a JSON object that contains a list of objects; returns Null for the nested object


This is my first attempt of working with JSON deserialization. I have read many of the posts in Stackoverflow, non of the suggested solutions could match my issue, so my apology in advance. I have created the following objects:

public class Item
{
    public int ID { get; set; }
    public int LSum { get; set; }
    public int YSum { get; set; }
    public int TSum { get; set; }
    public int NSum { get; set; }
    public int MemberId { get; set; }
}

public class Something
{
    public int Id { get; set; }
    public string Phone { get; set; }
    public bool ExistingMember { get; set; }
    public IList<Item> Item { get; set; }
}

And when deserialize the JSON it looks like following: The follwoing JSON what I expect it to be:

    {
   "Id":62,
   "Phone":"07",
   "ExistingMember":true,
   "Item":[
      {
         "ID":42,
         "LSum":0,
         "YSum":0,
         "TSum":0,
         "NSum":0,
         "MemberId":12
      }
   ]
}

However the following method it

   some= JsonConvert.DeserializeObject<something>(someResponse);

It prints the json like the following: The following JSON is what is "someResponse" return,

{
   "Id":62,
   "Phone":"07",
   "ExistingMember":true,
   "Item":null
}

What am I missing that the item list is return null?


Solution

  • If you want to deserialize json string which in your case is someResponse variable, then you are doing it right.

    To check your code I created a JSON file with a file.json name and put the following on it:

    {
      "Id": 62,
      "Phone": "07",
      "ExistingMember": true,
      "Item": [
        {
          "ID": 42,
          "LSum": 0,
          "YSum": 0,
          "TSum": 0,
          "NSum": 0,
          "MemberId": 12
        }
      ]
    }
    

    Then below lines of code take the content of JSON file (which in your case is the content of someResponse) and deserialize it into c# object of Something type:

    string jsonFilePath = @"C:\test\file.json";
    
    var some = JsonConvert.DeserializeObject<Something>(File.ReadAllText(jsonFilePath));
    

    Then print the ID property of each element of Item list:

    foreach(var item in some.Item)
    {
        if (item != null)
        {
            Console.WriteLine($"item ID = {item.ID}");
        }               
    }
    

    The output:

    item ID = 42
    

    So, it is quite possible that someResponse just does not have an Item and looks like this:

    {
      "Id": 62,
      "Phone": "07",
      "ExistingMember": true
    }
    

    UPDATE:

    Also I tried like this:

    var someResponse = @"{
      'Id': 62,
      'Phone': '07',
      'ExistingMember': true,
      'Item':[
        {
          'ID': 42,
          'LSum': 0,
          'YSum': 0,
          'TSum': 0,
          'NSum': 0,
          'MemberId': 12
        }
      ]
    }
    ";
    
    var some = JsonConvert.DeserializeObject<Something>(someResponse);
    

    And some has a Item list with 1 element enter image description here