反序列化动态JSON响应

3

我正在使用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,但是该怎么做呢?

1个回答

2

声明ExternalUrls为外部链接。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接