locked
C# JSON DeserializeObject Return NULL for embedded JSON and List of JSON objects RRS feed

  • Question

  • User956626884 posted

    Hi,

    I wrote a C# code to deserialize a JSON string of json record array. I used the command and noticed that embedded JSON object or List of json objects are returning NULL. Here is my command

    var result = Json.Convert.Deserialize<List<MyViewModel>>(
      @"[{
        'myId': 1,
        'myName': 'Test',
        'myJsonObject' : {
          'pName' : 'test1',
          'pOtherName' : 'test2',
          'phones' : {
            'landline' : 2222222222,
            'cell' : 3333333333,
          }
        }
    }]);

    I read other posts and they say that the deserilialize call will work for the entire json string but it does not work on my example. I am trying to convert this JSON string to a List<MyViewModel> object. Here is my class

    public class MyViewModel
    {
      public int myId {get;set;}
      public string myName {get;set;}
      public MyJsonViewModel MyJsonObject {get;set;}
      
    }

    public class MyJsonViewModel 
    {
      public string pName {get;set;}
      public string pOtherName {get;set;}
      public PhoneViewModel phones {get;set;}
    }

    public class PhoneViewModel 
    {
      public string landline {get;set;}
      public string cell {get;set;}
    }

    Thanks for any help.

    Wednesday, April 14, 2021 12:02 PM

All replies

  • User753101303 posted

    Hi,

    AFAIK json uses " rather than ' (double quotes rather than single quotes). Also cases mismatch and an extra. Not sure which deserializerr you are using but I would expect an exception rather thhan siliently failing and returning bull values ?

    At worst create the object you want, serialize this object and copy/paste this in your code (I would use an objet initializer).  I basically never create a json string by hand.

    Wednesday, April 14, 2021 12:34 PM
  • User475983607 posted

    Please read the NewtonSoft documentation.

    http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

    var result = JsonConvert.DeserializeObject<List<MyViewModel>>(
    @"[{
        'myId': 1,
        'myName': 'Test',
        'myJsonObject' : {
            'pName' : 'test1',
            'pOtherName' : 'test2',
            'phones' : {
            'landline' : 2222222222,
            'cell' : 3333333333,
            }
        }
    }]");
        public class MyViewModel
        {
            public int myId { get; set; }
            public string myName { get; set; }
            public Myjsonobject myJsonObject { get; set; }
        }
    
        public class Myjsonobject
        {
            public string pName { get; set; }
            public string pOtherName { get; set; }
            public Phones phones { get; set; }
        }
    
        public class Phones
        {
            public long landline { get; set; }
            public long cell { get; set; }
        }

    Wednesday, April 14, 2021 12:37 PM
  • User753101303 posted

    And if serializing again Newtonsoft generates (with idented formatting for clarity)  :

    [
      {
        "myId": 1,
        "myName": "Test",
        "MyJsonObject": {
          "pName": "test1",
          "pOtherName": "test2",
          "phones": {
            "landline": "2222222222",
            "cell": "3333333333"
          }
        }
      }
    ]

    Note the case mismatch for  My and the use of strings for phone numbers. It may or may not matter but it can introduce subtle bugs for example if you try to use both payload with JavaScript which is case sensitive, the same payload could fail with another deserialiazer because of the single quotes which is likely jsut a "conveniance" in Newtonsoft to allow as well the JavaScript syntax ?

    And so I never built myself a json string but always use the result of an actual serialization. Not 100% sure about the  context bue here I would use a C# object initliazer if you really want to bulild an hardcoded collection.

    Wednesday, April 14, 2021 1:32 PM
  • User956626884 posted

    I tried something like this but it still giving NULL for the nested objects

    List<MyViewModels> myViewModels = dbRepository.GetViewModels().ToList();

    var result = JsonConvert.SerializeObject(myViewModels); // I see the nested objects

    however the nested objects are null when I do this

    var result2 = JsonConvert.DeserializeObject<List<MyViewModel>>(result.toString());

    I am using Newtonsoft.json

    will try json.net to see if there is any difference

    Wednesday, April 14, 2021 3:38 PM
  • User475983607 posted

    The code I shared works as expected.  There must be somehting wrong with your code which we cannot see.  Try using the Visual Studio debugger to figure out what is going on with your logic.

    Wednesday, April 14, 2021 3:48 PM
  • User753101303 posted

    Make sure to copy paste the EXACT code to avoid typo. The {;} button allows to insert code in a legible way.

    Do you have both a ViewModels and ViewModel class? You tried variations of your class for testing? I see that you have List<MyViewModels> and then List<MyViewModel>? Is this a typo or really what you have in your code?

    Wednesday, April 14, 2021 8:16 PM