Search code examples
c#system.text.json

Converting JSON data from an API to a C# object


I'm trying to convert JSON data from an API to a c# object and I am getting the following error:

The JSON value could not be converted to CountriesDemo.Models.CountryModel. Path: $ | LineNumber: 0 | BytePositionInLine: 1

My function:

public static async Task getCountryData()
{
    var apiURL = "https://restcountries.com/v3.1/alpha/tto";

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var jsonResults = await client.GetStringAsync(apiURL);
    CountryModel? cshrpObj = System.Text.Json.JsonSerializer.Deserialize< CountryModel >(jsonResults);
    Console.WriteLine(cshrpObj?.name);
}

C# class:

public class CountryModel
{
    public string? name { get; set; }
    public string tld { get; set; } = null!;
    public string cca2 { get; set; } = null!;
    public string ccn3 { get; set; } = null!;
    public string cca3 { get; set; } = null!;
    public string cioc { get; set; } = null!;
    public string currency { get; set; } = null!;
    public string idd { get; set; } = null!;
    public string capital { get; set; } = null!;
    public string altSpellings { get; set; } = null!;
    public string region { get; set; } = null!;
    public string subregion { get; set; } = null!;
    public string languages { get; set; } = null!;
    public string translations { get; set; } = null!;
    public string latlng { get; set; } = null!;
    public string demonym { get; set; } = null!;
    public string landlocked { get; set; } = null!;
    public string borders { get; set; } = null!;
    public string area { get; set; } = null!;
}

Snippet of API data

[
  {
    "name": {
        "common": "United States",
        "official": "United States of America",
        "nativeName": {
            "eng": {
                "official": "United States of America",
                "common": "United States"
            }
        }
    },
    "tld": [
        ".us"
    ],
    "cca2": "US",
    "ccn3": "840",
    "cca3": "USA",
    "cioc": "USA",
    "independent": true,
    "status": "officially-assigned",
    "unMember": true,
]

Solution

  • unfortunately your json model is not correct. For json you posted you need this model

        List<Country> myDeserializedClass = JsonSerializer.Deserialize<List<Country>>(jsonResults);
    
    public class Country
    {
        public Name name { get; set; }
        public List<string> tld { get; set; }
        public string cca2 { get; set; }
        public string ccn3 { get; set; }
        public string cca3 { get; set; }
        public string cioc { get; set; }
        public bool independent { get; set; }
        public string status { get; set; }
        public bool unMember { get; set; }
    }
    public class Eng
    {
        public string official { get; set; }
        public string common { get; set; }
    }
    
    public class Name
    {
        public string common { get; set; }
        public string official { get; set; }
        public NativeName nativeName { get; set; }
    }
    
    public class NativeName
    {
        public Eng eng { get; set; }
    }