0

Suppose I have JSON like this, how can I model my class for deserialization?

I have no problems to model class for standard attribute like "dummy" or normal arrays, but in this case, my "links" array is a list of items with different name ("addons", "conditions", "conversion", etc.).

"dummy": "1",
 "links": {
            "addons": {
                "href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/addons",
                "method": "GET"
            },
            "conditions": {
                "href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/conditions",
                "method": "GET"
            },
            "conversions": {
                "href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/conversions",
                "method": "GET"
            },
            "list_prices": {
                "href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/list-prices",
                "method": "GET"
            },
            "mutual_exclusion": {
                "href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/mutually-exclusive-offers",
                "method": "GET"
            },
            "prerequisites": {
                "href": "/16071d9f-efec-4282-a42e-a495eea76ae0/offers/031C9E47-4802-4248-838E-778FB1D2CC05/prerequisites",
                "method": "GET"
            },
            "product": {
                "href": "/16071d9f-efec-4282-a42e-a495eea76ae0/products/f245ecc8-75af-4f8e-b61f-27d8114de5f3",
                "method": "GET"
            }
        },
2
  • 4
    Have you seen json2csharp? Commented Nov 24, 2014 at 19:01
  • You could model links as Dictionary<string, LinkInfo>, where string is "addons", "conditions", "conversion", etc. And LinkInfo have fields href and method Commented Nov 24, 2014 at 19:03

2 Answers 2

2

Assuming you are specifically looking for the set of LinkTypes if you will, in your JSON, could you use something like the following, and execute the Deserialize on the RootObject?

Working dotNet Fiddle: https://dotnetfiddle.net/ZWSlK4

Check out the output on the Console pane on the fiddle page.

public class Link
{
    public string Href { get; set; }
    public string Method { get; set; }
}

public class Links
{
    [JsonProperty("addons")]
    public Link Addons { get; set; }
    [JsonProperty("conditions")]
    public Link Conditions { get; set; }
    [JsonProperty("conversions")]
    public Link Conversions { get; set; }
    [JsonProperty("list_prices")]
    public Link ListPrices { get; set; }
    [JsonProperty("mutual_exclusion")]
    public Link MutualExclusion { get; set; }
    [JsonProperty("prerequisites")]
    public Link Prerequisites { get; set; }
    [JsonProperty("product")]
    public Link Product { get; set; }
}


public class RootObject
{
    public string dummy { get; set; }
    public Links links { get; set; }
}

and then execute the Deserializer like so.

var myDummyLinksList = JsonConvert.DeserializeObject<RootObject>(jsonText);

where jsonText contains the json string you have listed in your example:

However, if you List of links objects is dynamic and the number of objects inside varies and you need to capture all of them, then you might have to write a custom Converter that inherits from the JsonConverter object. then use the answer that @mfarouk has posted.

I forked my dotNet Fiddle and implemented his solution and it works like a boss for the dynamic case!

Working dotNet Fiddle (dynamic case): https://dotnetfiddle.net/7bFcNM

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

It works well. Thank you! I was trying something similar with a List<Link> without success...
Glad it worked. I was working on writing the dynamic serializer also, if you want to capture these specific Link types into a List<Link>. If I get it done, I will update the post too. Also check out the fiddle I created => dotnetfiddle.net/ZWSlK4
1

the links attribute could be parsed as key, value dictionary , the class can be like

public class JSONClass
{
    public string dummy { get; set; }
    public Dictionary<string, Link> links;

    public class Link
    {
        public string Href { get; set; }
        public string Method { get; set; }
    }
}

then de-serialized as

var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JSONClass>(JSON);

1 Comment

This works for the dynamic case! I just plugged this code in a FORK of my dotNet Fiddle answer. Check it out here => dotnetfiddle.net/7bFcNM

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.