2011-10-21 8 views
4

JSONをカスタムオブジェクトにデシリアライズしようとしていますが、すべてのプロパティがnullに設定されています。誰かが間違ったことを見ますか?デシリアライズされたオブジェクトはすべての値が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; 

私のクラスはJSON.NETはオプトインシリアライズライブラリです

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

これは言語であるように、あまりにもそれらのオブジェクトをデシリアライズjson.net? –

答えて

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構造に含まれるものとしてフラグを設定するための属性が必要です。

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の構造を見ると、ルートノードとして機能するもう1つのクラスが必要で、Keyオブジェクトのコレクション/配列である「Keys」プロパティを持っています。 –

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

} 

内部修飾子を取り除いた後、serlizationステップ

+1

内部修正を削除する代わりに、[JsonProperty] -Attribute:https://stackoverflow.com/a/39380844/4792869を追加することができます – Breeze

関連する問題