简体   繁体   中英

deserializing dynamic JSON response

I'm using the Newtonsoft Json.NET API to parse JSON responses.

I'm having following JSON:

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

Both Keys "spotify" and "another" are dynamic, which means their name could change with the next reponse. Also there is no fixed lenght, there always could be more or less entries in "external_urls"

trying to parse it into following object:

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

How would I get Json.NET to deserialize the Key-Name to public String Key ? So I would have Key = "spotify" and Key = "another"

And I would need to use a List or IEnumerable, but how if it's a dynamic object which always can changes its size and is not an array ?

declare ExternalUrls as

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM