【问题标题】:deserializing dynamic JSON response反序列化动态 JSON 响应
【发布时间】:2014-07-09 21:15:52
【问题描述】:

我正在使用 Newtonsoft Json.NET API 来解析 JSON 响应。

我有以下 JSON:

{
    "country" : "DE",
    "external_urls": 
    {
        "spotify" : "https://open.spotify.com/user/123",
        "another" : "https://open.spotify.com/user/1232"
    }
}

“spotify”和“another”两个键都是动态的,这意味着它们的名称可能会随着下一次响应而改变。也没有固定的长度,“external_urls”中总是可能有或多或少的条目

试图将其解析为以下对象:

public class FullProfileResponse
{
    [JsonProperty("country")]
    public String Country { get; set; }
    [JsonProperty("external_urls")]
    public ExternalUrls ExternalUrls { get; set; }
}
public class ExternalUrls
{
    public String Key { get; set; }
    public String Value { get; set; }
}

如何让 Json.NET 将 Key-Name 反序列化为 public String Key?所以我会有Key = "spotify"Key = "another"

我需要使用 List 或 IEnumerable,但是如果它是一个动态对象,它总是可以改变它的大小而不是一个数组,该怎么办? ?

【问题讨论】:

    标签: c# json json.net deserialization


    【解决方案1】:

    声明 ExternalUrls

     [JsonProperty("external_urls")]
     public Dictionary<string,string> ExternalUrls { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2018-08-21
      • 1970-01-01
      • 2021-09-14
      • 2019-05-16
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多