反序列化对象将所有值设置为空

时间:2011-10-21 18:34:25

标签: c# json c#-4.0 json.net deserialization

我正在尝试将JSON反序列化为自定义对象,但我的所有属性都设置为null并且不确定发生了什么。有没有人看错了什么?

JSON示例

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

这是我的代码,其中strResponseValid是上面的JSON。

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

这是我的课程

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

5 个答案:

答案 0 :(得分:5)

在我的情况下,这是因为我的目的地类型为这些属性设置了内部(或私人)设置修饰符。

public class Summary{

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

}

删除内部修饰符后,json.net将这些对象反序列化,就像序列化步骤一样

答案 1 :(得分:4)

您的JSON有一个外部对象,其中包含一组Key对象。以下代码有效(我测试过):

    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 :(得分:1)

您不需要将AttributeList定义为类中的列表,只需调用PropertiesList Class,如下所示。

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

然后尝试使用以下命令反序列化:

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

答案 3 :(得分:1)

这里的问题是你将Keys定义为一个类,当它实际上是一个属性时。

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 :(得分:0)

JSON.NET是一个可选的序列化库。对象中的属性需要属性才能将它们标记为包含在JSON结构中。

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