6

We are using an external API that returns its results using the object name "Data". This "Data" can represent two different objects -one in the form of an json object array and another int the form of a single json object.

I have a created two separate c# classes that represents the JSON object data and a c# root object class to capture the JSON objects when being converted using JsonConvert.DeserializeObject...How would one go about representing these objects correctly in C# see below examples of results:

below is example of 1 resulting API Call

{
success:true,
Data:[{id:1,name:"Paul"},{id:2,name:"neville"},{id:3,name:"jason"}]
}

public class User
{
    public int id { get; set; }
    public string name { get; set; }
}

public class ApiResponse
{
    public bool success { get; set; }
    public List<User> Data { get; set; }
}

Below is example 2 of resulting API call

{
    success:true,
    Data:{id:1,classSize:30,minAge:25, maxAge:65}
}
public class AgeClass
{
    public int id { get; set; }
    public int classSize { get; set; }
    public int minAge { get; set; }
    public int maxAge { get; set; }
}

public class ApiResponse
{
    public bool success { get; set; }
    public AgeClass Data { get; set; }
}

How would I construct the ApiResponse Class to cater for the Generic "Data" object json string returned - so that I can use the generic "JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync(), Settings);"

2
  • go to this link...stackoverflow.com/questions/44545955/… Commented Jan 28, 2019 at 9:41
  • The problem I'm facing is that I want 1 ApiResponse class that has a Generic Property "Data" that can cater for both results returned by the API call Commented Jan 28, 2019 at 9:52

2 Answers 2

5

I would propose to consider you actually have 2 types of JSON response. You also can inherit them from base ApiResponse class:

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string json1 = @"{
                                ""success"":true,
                                ""Data"":[{id:1, name:""Paul""},{id:2,name:""neville""},{id:3,name:""jason""}]
                            }";

            string json2 = @"{
                                ""success"":true,
                                ""Data"":{id:1,classSize:30,minAge:25, maxAge:65}
                             }";

            string j = json1; //json2;

            JObject jo = JObject.Parse(j);

            ApiResponse parsed;

            if (jo["Data"].Type == JTokenType.Array)
                parsed = jo.ToObject<ApiUsersResponse>();
            else
                parsed = jo.ToObject<ApiAgeResponse>();

            Console.ReadKey();
        }        
    }

    class User
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    class AgeClass
    {
        public int id { get; set; }
        public int classSize { get; set; }
        public int minAge { get; set; }
        public int maxAge { get; set; }
    }

    class ApiResponse
    {
        public bool success { get; set; }
    }

    class ApiUsersResponse : ApiResponse
    {
        public List<User> Data { get; set; }
    }

    class ApiAgeResponse : ApiResponse
    {            
        public AgeClass Data { get; set; }
    }     
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - I have used the above approach to accomplish what I needed
4

You could just make your class generic by adding generic type parameter for Data property:

public class ApiResponse<T>
{
    public bool success { get; set; }
    public T Data { get; set; }
}

And then use it when deserializing:

  var result = JsonConvert.DeserializeObject<ApiResponse<AgeClass>>(myJson ...);
  var result = JsonConvert.DeserializeObject<ApiResponse<List<User>>>(myJson ...);

Comments

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.