-1

I'm creating a library wrapper which handle all API requests of this football site. When I call an endpoint, this response structure is returned:

{
    "get": "countries",
    "parameters": [],
    "errors": [],
    "results": 161,
    "paging": {
        "current": 1,
        "total": 1
    },
    "response": [
        {
            "name": "Albania",
            "code": "AL",
            "flag": "https://media.api-sports.io/flags/al.svg"
        },
        {
            "name": "Algeria",
            "code": "DZ",
            "flag": "https://media.api-sports.io/flags/dz.svg"
        },
        {
            "name": "Andorra",
            "code": "AD",
            "flag": "https://media.api-sports.io/flags/ad.svg"
        },
    ]
}

The example above, is the /countries endpoint. I have used Quicktype to generate the schema structure, and I got this:

using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace FootballAPI.Models
{
    public partial class Temperatures
    {
        [JsonProperty("get")]
        public string Get { get; set; }

        [JsonProperty("parameters")]
        public object[] Parameters { get; set; }

        [JsonProperty("errors")]
        public object[] Errors { get; set; }

        [JsonProperty("results")]
        public long Results { get; set; }

        [JsonProperty("paging")]
        public Paging Paging { get; set; }

        [JsonProperty("response")]
        public Response[] Response { get; set; }
    }

    public partial class Paging
    {
        [JsonProperty("current")]
        public long Current { get; set; }

        [JsonProperty("total")]
        public long Total { get; set; }
    }

    public partial class Response
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("code")]
        public string Code { get; set; }

        [JsonProperty("flag")]
        public Uri Flag { get; set; }
    }
}

What I'm trying to do is have a base structure schema which contains everything except the response property. In the example above response must be an instance of Country which is:

public class Country 
{
    public string name { get; set; }
    public string code { get; set; }
    public string flag { get; set; }
}

how can I tell to the base schema above to use Country as T object? I should be able to correctly parse everything with Newtonsoft.JSON:

JsonConvert.DeserializeObject<Country>;

I have different methods like GetCountries() that only pass the query string, in that case /countries. So I know the specific type I expect "response" to be at that time.

3
  • Do you know the type Country statically, in advance when you make the call to sonConvert.DeserializeObject? Or is the returned result polymorphic in the sense you don't know in advance what type gets returned and you will need to check the value of "get": "countries"? Commented Aug 21, 2021 at 16:34
  • @dbc I have different methods like GetCountries() that only pass the query string, in that case /countries. So I can also pass the type, or I though to convert the response directly in the GetCountries() method, so: GetCountries() call the network client which return the structure above, then I parse the structure of response inside GetCountries(). I don't know if there is a better solution for this Commented Aug 21, 2021 at 16:56
  • Then is this ResponseList<T> all you're looking for? dotnetfiddle.net/XXt0Eo? Commented Aug 21, 2021 at 17:11

1 Answer 1

-1

just try these classes


public class Country
{
    public string name { get; set; }
    public string code { get; set; }
    public string flag { get; set; }
}

public class Countries
{
    public string get { get; set; }
    public List<object> parameters { get; set; }
    public List<object> errors { get; set; }
    public int results { get; set; }
    public Paging paging { get; set; }
    public List<Country> response { get; set; }
}
public class Paging
{
    public int current { get; set; }
    public int total { get; set; }
}

you can use

JsonConvert.DeserializeObject<Countries>(json);
Sign up to request clarification or add additional context in comments.

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.