Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 333
Jonas Dellinger Avatar asked Aug 31 '26 07:08

Jonas Dellinger


1 Answers

declare ExternalUrls as

 [JsonProperty("external_urls")]
 public Dictionary<string,string> ExternalUrls { get; set; }
like image 192
L.B Avatar answered Sep 02 '26 20:09

L.B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!