Jonas Dellinger
Jonas Dellinger

Reputation: 1364

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 ?

Upvotes: 3

Views: 298

Answers (1)

L.B
L.B

Reputation: 116108

declare ExternalUrls as

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

Upvotes: 2

Related Questions