2011-10-21 114 views
4

我試圖將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; } 
    } 
+0

哪種語言是什麼? –

回答

3

您的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); 
} 
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; } 
} 
+0

另外,看看你的JSON的結構,你將需要一個類來充當根節點,並且有一個「Keys」屬性,它是一個Key對象的集合/數組。 –

+0

我一直使用Json.Net和未標記的屬性。您的評論(以及@ MikeRichards的回答如下)似乎是實際的問題。 –

+0

你說得對。我一直使用它作爲選入庫,但這是由班上的一個屬性決定的。 –

1

您不需要將PropertiesList定義爲類中的列表,只需調用PropertiesList類,如下所示。

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

這裏的問題是,你定義按鍵只是一個類,當它實際上是一個性質。

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

在我的情況,那是因爲我的目標類型有內部(或私人)設置修飾這些屬性。

public class Summary{ 

    public QuickStats Stats { get; internal set; } 
    public QuickStats Stats2 { get; set; } 

} 

消除內部修改後,JSON.NET反序列化這些對象太像serlization步

+1

除了刪除內部修改符之外,還可以添加[JsonProperty] - 屬性:https://stackoverflow.com/a/39380844/4792869 – Breeze