使用Json.NET反序列化JSON动态命名字段

时间:2014-04-03 08:04:16

标签: c# .net json visual-studio json.net

我从服务器那里得到JSON格式的答案(uid1,uid2 ... uidN - 是来自服务器的动态命名字段):

{
   "get_message_state":
   {
      "uid1":"some text 1",
      "uid2":"some text 2",
      ...
      "uidN":"some text N"
   },
   "status":"OK_Operation_Completed"
}

当我尝试描述要从服务器反序列化json响应的类时,我遇到了 get_message_state 字段的问题。如何在课堂上描述这个领域?

public class MessageStateResponse
{
   [JsonProperty(PropertyName = "status", Order = 2)]
   public string Status { get; set; }
   [JsonProperty(PropertyName = "get_message_state", Order = 1)]
   public Msg MessageState { get; set; } //??????????
}

public class Msg
{
   [JsonProperty]
   public Dictionary<string, string> Fields { get; set; } //??????????
}

1 个答案:

答案 0 :(得分:1)

您不必将字典包装到Msg对象。

public class MessageStateResponse
{
   [JsonProperty(PropertyName = "status", Order = 2)]
   public string Status { get; set; }
   [JsonProperty(PropertyName = "get_message_state", Order = 1)] 
   public Dictionary<string, string> Fields { get; set; }
}