5

I have a simple JSON like this:

{
    "id": 123,
    "name": "BaseName",
    "variation": { "name": "VariationName" }
}

Is there a simple way to map it with JSON.NET deserialization to:

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string VariationName { get; set; }
}

I can probably do it with a custom converter, but I hoped there would be a simpler way by annotating the class with attributes which would give instructions to deserialize the variation object just using the one property.

3

1 Answer 1

3

You could set up a class for variation and make VariationName a get-only property

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Variation variation { get; set; }
    public string VariationName { get { return variation.VariationName; } }
}

class variation 
{
    public string name { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

My example is a bit simplified, so it's not that elegant but this will probably the simplest way to go. I was curious if there is any trick I am missing. I'll see if there's any other suggestion and accept this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.