2014-07-09 193 views
3

我使用Newtonsoft Json.NET API來解析JSON響應。反序列化動態JSON響應

我有以下JSON:

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

兩個鍵「Spotify的」和「其他」是動態的,這意味着它們的名稱可能與下一個改變效應初探。也沒有固定的lenght,總是有可能會或多或少條目「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反序列化密鑰 - 姓名爲public String Key?因此,我將有Key = "spotify"Key = "another"

而且我會需要使用一個列表或IEnumerable的,但如何,如果它是一個動態的對象,它總是可以改變它的大小,而不是一個數組 ?

回答

2

聲明ExternalUrls作爲

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