Deserialized Object Has All Values Set to Null

﹥>﹥吖頭↗ 提交于 2019-12-22 08:03:29

问题


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; }
    }

回答1:


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);
 }



回答2:


in my case it was because of my destination type have internal (or private) set modifiers for those properties .

public class Summary{

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

}

after removing internal modifier, json.net deserialize those objects too like the serialization step




回答3:


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; }
}



回答4:


You don't need to define PropertiesList as a list in the class just call the PropertiesList Class as below.

public class Keys
    {
        public string RegistrationKey { get; set; }
        public string ValidationStatus { get; set; }
        public string ValidationDescription { get; set; }
        public PropertiesList  PropertiesList { get; set; }
    }

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

Then try using the following to deserialize:

keys myDeserializedObjValid = JsonConvert.DeserializeObject<keys>(strResponseValid);



回答5:


JSON.NET is an opt-in serialization library. The properties in your objects require attributes in order to flag them as being included in the JSON structure.

public class Keys
{
    [JsonProperty(PropertyName = "RegistrationKey")]
    public string RegistrationKey { get; set; }

    [JsonProperty(PropertyName = "ValidationStatus")]
    public string ValidationStatus { get; set; }

    [JsonProperty(PropertyName = "ValidationDescription")]
    public string ValidationDescription { get; set; }

    [JsonProperty(PropertyName = "Properties")]
    public List<Properties> PropertiesList { get; set; }
}

public class Properties
{
    [JsonProperty(PropertyName = "Key")]
    public string Key { get; set; }

    [JsonProperty(PropertyName = "Value")]
    public string Value { get; set; }
}


来源:https://stackoverflow.com/questions/7853755/deserialized-object-has-all-values-set-to-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!