Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialized Object Has All Values Set to Null

I'm trying to deserialize JSON into a custom object but all my properties are set to null and not sure what's going on. Does anyone see anything wrong?

JSON Example

{
"Keys": [
    {
        "RegistrationKey": "asdfasdfa",
        "ValidationStatus": "Valid",
        "ValidationDescription": null,
        "Properties": [
            {
                "Key": "Guid",
                "Value": "i0asd23165323sdfs68661358"
            }
        ]
    }
 ]
}

Here is my Code, where strResponseValid is the JSON above.

Keys myDeserializedObjValid = (Keys)JsonConvert.DeserializeObject(strResponseValid, typeof(Keys));
validationStatusValid = myDeserializedObjValid.ValidationStatus;

Here are my classes

    public class Keys
    {
        public string RegistrationKey { get; set; }
        public string ValidationStatus { get; set; }
        public string ValidationDescription { get; set; }
        public List<Properties> PropertiesList { get; set; }
    }

    public class Properties
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
like image 452
aahrens Avatar asked Oct 21 '11 18:10

aahrens


People also ask

How do I ignore null values in json Deserializing?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

Can json have null values?

"JSON's value called null can be set on any type of data including arrays, objects, number and boolean types" is 100% MATCH the standard http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf.

Can we deserialize null?

Deserialization ignores nulls in the json and doesn't update the property.


3 Answers

check if the destination type has internal (or private) set modifier for those properties, even if the property has public access modifier itself.

public class Summary{

     public Class2 Prop1 { get; internal set; }
     public Class1 prop2 { get; set; }

}

Solution: remove those explicit internal access modifier and make them writable from outside

like image 105
Iman Avatar answered Oct 19 '22 23:10

Iman


Your JSON has an outer object which contains a collection of Key objects. The following code works (I tested it):

    class KeyWrapper
    {
        public List<Key> Keys { get; set; }
    }

    class Key
    {
        public string RegistrationKey { get; set; }
        public string ValidationStatus { get; set; }
        public string ValidationDescription { get; set; }
        public List<Properties> Properties { get; set; }
    }

    public class Properties
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

    public void DeserializeKeys()
    {            
        const string json = @"{""Keys"": 
            [
                {
                    ""RegistrationKey"": ""asdfasdfa"",
                    ""ValidationStatus"": ""Valid"",
                    ""ValidationDescription"": null,
                    ""Properties"": [
                        {
                            ""Key"": ""Guid"",
                            ""Value"": ""i0asd23165323sdfs68661358""
                        }
                    ]
                 }
             ]
         }";

        var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapper>(json);
 }
like image 21
Frank Avatar answered Oct 20 '22 00:10

Frank


The problem here is that you're defining Keys as just a class, when it's actually a property.

public class Response
{
    public Keys Keys { get; set; }
}

public class Keys
{
    public string RegistrationKey { get; set; }
    public string ValidationStatus { get; set; }
    public string ValidationDescription { get; set; }
    public List<Properties> PropertiesList { get; set; }
}

public class Properties
{
    public string Key { get; set; }
    public string Value { get; set; }
}
like image 43
Mike Richards Avatar answered Oct 20 '22 00:10

Mike Richards