3

I have JSON response from an API which might contain a different content type (different content type requires a different object). This is how the JSON from the API response looks like:

{
    "Id": 2,
    "UserContent": [{
            "Id": 10,
            "ContentType": "1",
            "Content": [{
                    "Id": "7",
                    "Question": "Question 1?",
                    "Answer": "Answer 1."
                }, {
                    "Id": "1",
                    "Question": "Question 2?",
                    "Answer": "Answer 2."
                }, {
                    "Id": "6",
                    "Question": "Question 3?",
                    "Answer": "Answer 3."
                }
            ]
        }, {
            "Id": 11,
            "ContentType": "2",
            "Content": {
                "TestScore": "73",
                "TestIsPassed": false
            }
        }
    ]
}

And the following objects:

public class ContentDataTransferModel
{
    public int Id { get; set; }
    public List<ContentDataModel> UserContent { get; set; }
}

public class ContentDataModel
{
    public int Id { get; set; }
    public string ContentType { get; set; }

    public WhatShouldBeTheContentType? Content { get; set; }
}

public class ContentDataType1
{
    public int Id { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
}

public class ContentDataType2
{
    public string TestScore { get; set; }
    public bool TestIsPassed { get; set; }
}

How can I dynamically switch between the object types when I am handling the response or is that even possible? I tried setting the Content property to object, dynamic an interface but I am out of ideas and I don't really know if it's possible to handle such a scenario.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
IvanD
  • 157
  • 4
  • 12
  • 2
    Possible duplicate of [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/questions/19307752/deserializing-polymorphic-json-classes-without-type-information-using-json-net) – Carsten Führmann Nov 20 '18 at 17:14
  • 1
    It seems the `Content` property should be an `object`. Or the `ContentDataType1` and `ContentDataType1` must be descendants of a same class. – Alexander Nov 20 '18 at 17:18
  • What serializer are you using? Json.Net or something else? – Brian Rogers Nov 20 '18 at 17:52
  • @BrianRogers I am accepting the `ContentDataTransferModel` in my Post action in the controller – IvanD Nov 20 '18 at 18:32
  • @Alexander I tried with making it an `object`, but I am not able to convert the `value` on the property setter can you give me an example please about your second idea? – IvanD Nov 20 '18 at 18:33
  • 1
    @CarstenFührmann Thanks, I will give it a try and give a feedback :) – IvanD Nov 20 '18 at 18:35
  • 1
    @IvanD What I meant was, what framework are you using? ASP.NET MVC or WebApi, or something else? – Brian Rogers Nov 20 '18 at 20:23
  • @BrianRogers ASP.NET MVC – IvanD Nov 21 '18 at 07:01
  • @CarstenFührmann nope, not working like that – IvanD Nov 21 '18 at 08:26

0 Answers0