Resolved API call from c# deserialize problem

stereochoc

Member
Joined
Jul 24, 2023
Messages
5
Programming Experience
10+
Hello, i just try to get data from an api with a list of country and would like to fill a DropDownList with the data.

I have create a class
Class for the data deserialise:
 class CountryList
    {
        public string id { get; set; }
        public string text { get; set; }
        public string textEN { get; set; }
    }

then i use this code to get data from the api. I have the data getting correctly but i have an error for deserialise it

Code for getting data from api:
var client = new HttpClient();
            string json = "";
            client.BaseAddress = new Uri("THE URL OF MY API");

            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var response = client.PostAsync("/external/api/country", content).Result;
            if (response.IsSuccessStatusCode)
            {
                var responseContent = response.Content.ReadAsStringAsync().Result;

                var options = new JsonSerializerOptions
                {
              
                };
                var postResponse = System.Text.Json.JsonSerializer.Deserialize<CountryList>(responseContent, options);


                 // CountryList postResponse = JsonConvert.DeserializeObject<CountryList>(responseContent);    I have an error with this too



            }
            else
            {
                // print error
            }

the error is on line 16.

In the responseContent i have value ok.

Here is the value :

Value in the responseContent:
"[{\"id\":75,\"text\":\"France\",\"textEN\":\"France\"},{\"id\":3,\"text\":\"Afghanistan\",\"textEN\":\"Afghanistan\"},{\"id\":247,\"text\":\"Afrique du Sud\",\"textEN\":\"South Africa\"},{\"id\":6,\"text\":\"Albanie\",\"textEN\":\"Albania\"},{\"id\":62,\"text\":\"Alg\\u00e9rie\",\"textEN\":\"Algeria\"},{\"id\":57,\"text\":\"Allemagne\",\"textEN\":\"Germany\"},{\"id\":1,\"text\":\"Andorre\",\"textEN\":\"Andorra\"},{\"id\":8,\"text\":\"Angola\",\"textEN\":\"Angola\"},{\"id\":5,\"text\":\"Anguilla\",\"textEN\":\"Anguilla\"},{\"id\":9,\"text\":\"Antarctique\",\"textEN\":\"Antarctica\"},{\"id\":4,\"text\":\"Antigua-et-Barbuda\",\"textEN\":\"Antigua and Barbuda\"},{\"id\":193,\"text\":\"Arabie saoudite\",\"textEN\":\"Saudi Arabia\"},{\"id\":10,\"text\":\"Argentine\",\"textEN\":\"Argentina\"},{\"id\":7,\"text\":\"Arm\\u00e9nie\",\"textEN\":\"Armenia\"},{\"id\":14,\"text\":\"Aruba\",\"textEN\":\"Aruba\"},{\"id\":13,\"text\":\"Australie\",\"textEN\":\"Australia\"},{\"id\":12,\"text\":\"Autriche\",\"textEN\":\"Austria\"},{\"id\":16,\"text\":\"Azerba\\u00efdjan\",\"textEN\":\"Azerbaijan\"},{\"id\":32,\"text\":\"Bahamas\",\"textEN\":\"Bahamas\"},{\"id\":23,\"text\":\"Bahre\\u00efn\",\"textEN\":\"Bahrain\"},{\"id\":19,\"text\":\"Bangladesh\",\"textEN\":\"Bangladesh\"},{\"id\":18,\"text\":\"Barbade\",\"textEN\":\"Barbados\"},{\"id\":36,\"text\":\"B\\u00e9larus\",\"textEN\":\"Belarus\"},{\"id\":20,\"text\":\"Belgique\",\"textEN\":\"Belgium\"},{\"id\":37,\"text\":\"Belize\",\"textEN\":\"Belize\"},{\"id\":25,\"text\":\"B\\u00e9nin\",\"textEN\":\"Benin\"},{\"id\":27,\"text\":\"Bermudes\",\"textEN\":\"Bermuda\"},{\"id\":33,\"text\":\"Bhoutan\",\"textEN\":\"Bhutan\"},{\"id\":29,\"text\":\"Bolivie\",\"textEN\":\"Bolivia\"},{\"id\":17,\"text\":\"Bosnie-Herz\\u00e9govine\",\"textEN\":\"Bosnia and Herzegovina\"},{\"id\":35,\"text\":\"Botswana\",\"textEN\":\"Botswana\"},{\"id\":31,\"text\":\"Br\\u00e9sil\",\"textEN\":\"Brazil\"},{\"id\":28,\"text\":\"Brunei Darussalam\",\"textEN\":\"Brunei Darussalam\"},{\"id\":22,\"text\":\"Bulgarie\",\"textEN\":\"Bulgaria\"},{\"id\":21,\"text\":\"Burkina Faso\",\"textEN\":\"Burkina Faso\"},{\"id\":24,\"text\":\"Burundi\",\"textEN\":\"Burundi\"},{\"id\":117,\"text\":\"Cambodge\",\"textEN\":\"Cambodia\"},{\"id\":47,\"text\":\"Cameroun\",\"textEN\":\"Cameroon\"},{\"id\":38,\"text\":\"Canada\",\"textEN\":\"Canada\"},{\"id\":52,\"text\":\"Cap-Vert\",\"textEN\":\"Cape Verde\"},{\"id\":46,\"text\":\"Chili\",\"textEN\":\"Chile\"},{\"id\":48,\"text\":\"Chine\",\"textEN\":\"China\"},{\"id\":55,\"text\":\"Chypre\",\"textEN\":\"Cyprus\"},{\"id\":49,\"text\":\"Colombie\",\"textEN\":\"Colombia\"},{\"id\":119,\"text\":\"Comores\",\"textEN\":\"Comoros\"},{\"id\":42,\"text\":\"Congo\",\"textEN\":\"Congo\"},{\"id\":40,\"text\":\"Congo, R\\u00e9publique d\\u00e9mocratique du\",\"textEN\":\"Congo, Democratic Republic of\"},{\"id\":121,\"text\":\"Cor\\u00e9e du Nord\",\"textEN\":\"North Korea\"},{\"id\":122,\"text\":\"Cor\\u00e9e du Sud\",\"textEN\":\"South Korea\"},{\"id\":50,\"text\":\"Costa Rica\",\"textEN\":\"Costa Rica\"},{\"id\":44,\"text\":\"C\\u00f4te d'Ivoire\",\"textEN\":\"C\\u00f4te d'Ivoire\"},{\"id\":98,\"text\":\"Croatie\",\"textEN\":\"Croatia\"},{\"id\":51,\"text\":\"Cuba\",\"textEN\":\"Cuba\"},{\"id\":53,\"text\":\"Cura\\u00e7ao\",\"textEN\":\"Cura\\u00e7ao\"},{\"id\":59,\"text\":\"Danemark\",\"textEN\":\"Denmark\"},{\"id\":58,\"text\":\"Djibouti\",\"textEN\":\"Djibouti\"},{\"id\":60,\"text\":\"Dominique\",\"textEN\":\"Dominica\"},{\"id\":65,\"text\":\"\\u00c9gypte\",\"textEN\":\"Egypt\"},{\"id\":210,\"text\":\"El Salvador\",\"textEN\":\"El Salvador\"},{\"id\":2,\"text\":\"\\u00c9mirats arabes unis\",\"textEN\":\"United Arab Emirates\"},{\"id\":63,\"text\":\"\\u00c9quateur\",\"textEN\":\"Ecuador\"},{\"id\":67,\"text\":\"\\u00c9rythr\\u00e9e\",\"textEN\":\"Eritrea\"},{\"id\":68,\"text\":\"Espagne\",\"textEN\":\"Spain\"},{\"id\":64,\"text\":\"Estonie\",\"textEN\":\"Estonia\"},{\"id\":233,\"text\":\"\\u00c9tats-Unis\",\"textEN\":\"United States\"},{\"id\":69,\"text\":\"\\u00c9thiopie\",\"textEN\":\"Ethiopia\"},{\"id\":71,\"text\":\"Fidji\",\"textEN\":\"Fiji\"},{\"id\":70,\"text\":\"Finlande\",\"textEN\":\"Finland\"},{\"id\":76,\"text\":\"Gabon\",\"textEN\":\"Gabon\"},{\"id\":85,\"text\":\"Gambie\",\"textEN\":\"Gambia\"},{\"id\":79,\"text\":\"G\\u00e9orgie\",\"textEN\":\"Georgia\"},{\"id\":90,\"text\":\"G\\u00e9orgie du Sud et les \\u00eeles Sandwich du Sud\",\"textEN\":\"South Georgia and the South Sandwich Islands\"},{\"id\":82,\"text\":\"Ghana\",\"textEN\":\"Ghana\"},{\"id\":83,\"text\":\"Gibraltar\",\"textEN\":\"Gibraltar\"},{\"id\":89,\"text\":\"Gr\\u00e8ce\",\"textEN\":\"Greece\"},{\"id\":78,\"text\":\"Grenade\",\"textEN\":\"Grenada\"},{\"id\":84,\"text\":\"Groenland\",\"textEN\":\"Greenland\"},{\"id\":87,\"text\":\"Guadeloupe\",\"textEN\":\"Guadeloupe\"},{\"id\":92,\"text\":\"Guam\",\"textEN\":\"Guam\"},{\"id\":91,\"text\":\"Guatemala\",\"textEN\":\"Guatemala\"},{\"id\":81,\"text\":\"Guernesey\",\"textEN\":\"Guernsey\"},{\"id\":86,\"text\":\"Guin\\u00e9e\",\"textEN\":\"Guinea\"},{\"id\":88,\"text\":\"Guin\\u00e9e \\u00e9quatoriale\",\"textEN\":\"Equatorial Guinea\"},{\"id\":93,\"text\":\"Guin\\u00e9e-Bissau\",\"textEN\":\"Guinea-Bissau\"},{\"id\":94,\"text\":\"Guyana\",\"textEN\":\"Guyana\"},{\"id\":80,\"text\":\"Guyane fran\\u00e7aise\",\"textEN\":\"French Guiana\"},{\"id\":99,\"text\":\"Ha\\u00efti\",\"textEN\":\"Haiti\"},{\"id\":97,\"text\":\"Honduras\",\"textEN\":\"Honduras\"},{\"id\":95,\"text\":\"Hong Kong\",\"textEN\":\"Hong Kong\"},{\"id\":100,\"text\":\"Hongrie\",\"textEN\":\"Hungary\"},{\"id\":34,\"text\":\"\\u00cele Bouvet\",\"textEN\":\"Bouvet Island\"},{\"id\":54,\"text\":\"\\u00cele Christmas\",\"textEN\":\"Christmas Island\"},{\"id\":104,\"text\":\"\\u00cele de Man\",\"textEN\":\"Isle of Man\"},{\"id\":163,\"text\":\"\\u00cele Norfolk\",\"textEN\":\"Norfolk Island\"},{\"id\":124,\"text\":\"\\u00celes Ca\\u00efmans\",\"textEN\":\"Cayman Islands\"},{\"id\":39,\"text\":\"\\u00celes Cocos (Keeling)\",\"textEN\":\"Cocos (Keeling) Islands\"},{\"id\":45,\"text\":\"\\u00celes Cook\",\"textEN\":\"Cook Islands\"},{\"id\":15,\"text\":\"\\u00celes d'\\u00c5land\",\"textEN\":\"\\u00c5land Islands\"},{\"id\":74,\"text\":\"\\u00celes F\\u00e9ro\\u00e9\",\"textEN\":\"Faroe Islands\"},{\"id\":96,\"text\":\"\\u00celes Heard et McDonald\",\"textEN\":\"Heard and McDonald Islands\"},{\"id\":72,\"text\":\"\\u00celes Malouines\",\"textEN\":\"Falkland Islands\"},{\"id\":143,\"text\":\"\\u00celes Marshall\",\"textEN\":\"Marshall Islands\"},{\"id\":232,\"text\":\"\\u00celes mineures \\u00e9loign\\u00e9es des \\u00c9tats-Unis\",\"textEN\":\"United States Minor Outlying Islands\"},{\"id\":194,\"text\":\"\\u00celes Salomon\",\"textEN\":\"Solomon Islands\"},{\"id\":214,\"text\":\"\\u00celes Turks et Caicos\",\"textEN\":\"Turks and Caicos Islands\"},{\"id\":240,\"text\":\"\\u00celes Vierges am\\u00e9ricaines\",\"textEN\":\"Virgin Islands (U.S.)\"},{\"id\":239,\"text\":\"\\u00celes Vierges britanniques\",\"textEN\":\"Virgin Islands (British)\"},{\"id\":243,\"text\":\"\\u00celes Wallis-et-Futuna\",\"textEN\":\"Wallis and Futuna Islands\"},{\"id\":105,\"text\":\"Inde\",\"textEN\":\"India\"},{\"id\":101,\"text\":\"Indon\\u00e9sie\",\"textEN\":\"Indonesia\"},{\"id\":107,\"text\":\"Irak\",\"textEN\":\"Iraq\"},{\"id\":108,\"text\":\"Iran\",\"textEN\":\"Iran\"},{\"id\":102,\"text\":\"Irlande\",\"textEN\":\"Ireland\"},{\"id\":109,\"text\":\"Islande\",\"textEN\":\"Iceland\"},{\"id\":103,\"text\":\"Isra\\u00ebl\",\"textEN\":\"Israel\"},{\"id\":110,\"text\":\"Italie\",\"textEN\":\"Italy\"},{\"id\":112,\"text\":\"Jama\\u00efque\",\"textEN\":\"Jamaica\"},{\"id\":114,\"text\":\"Japon\",\"textEN\":\"Japan\"},{\"id\":111,\"text\":\"Jersey\",\"textEN\":\"Jersey\"},{\"id\":113,\"text\":\"Jordanie\",\"textEN\":\"Jordan\"},{\"id\":125,\"text\":\"Kazakhstan\",\"textEN\":\"Kazakhstan\"},{\"id\":115,\"text\":\"Kenya\",\"textEN\":\"Kenya\"},{\"id\":116,\"text\":\"Kirghizistan\",\"textEN\":\"Kyrgyzstan\"},{\"id\":118,\"text\":\"Kiribati\",\"textEN\":\"Kiribati\"},{\"id\":123,\"text\":\"Kowe\\u00eft\",\"textEN\":\"Kuwait\"},{\"id\":126,\"text\":\"Laos\",\"textEN\":\"Lao People's Democratic Republic\"},{\"id\":132,\"text\":\"Lesotho\",\"textEN\":\"Lesotho\"},{\"id\":135,\"text\":\"Lettonie\",\"textEN\":\"Latvia\"},{\"id\":127,\"text\":\"Liban\",\"textEN\":\"Lebanon\"},{\"id\":131,\"text\":\"Lib\\u00e9ria\",\"textEN\":\"Liberia\"},{\"id\":136,\"text\":\"Libye\",\"textEN\":\"Libya\"},{\"id\":129,\"text\":\"Liechtenstein\",\"textEN\":\"Liechtenstein\"},{\"id\":133,\"text\":\"Lituanie\",\"textEN\":\"Lithuania\"},{\"id\":134,\"text\":\"Luxembourg\",\"textEN\":\"Luxembourg\"},{\"id\":148,\"text\":\"Macao\",\"textEN\":\"Macau\"},{\"id\":144,\"text\":\"Mac\\u00e9doine\",\"textEN\":\"Macedonia\"},{\"id\":142,\"text\":\"Madagascar\",\"textEN\":\"Madagascar\"},{\"id\":158,\"text\":\"Malaisie\",\"textEN\":\"Malaysia\"},{\"id\":156,\"text\":\"Malawi\",\"textEN\":\"Malawi\"},{\"id\":155,\"text\":\"Maldives\",\"textEN\":\"Maldives\"},{\"id\":145,\"text\":\"Mali\",\"textEN\":\"Mali\"},{\"id\":153,\"text\":\"Malte\",\"textEN\":\"Malta\"},{\"id\":149,\"text\":\"Mariannes du Nord\",\"textEN\":\"Northern Mariana Islands\"},{\"id\":137,\"text\":\"Maroc\",\"textEN\":\"Morocco\"},{\"id\":150,\"text\":\"Martinique\",\"textEN\":\"Martinique\"},{\"id\":154,\"text\":\"Maurice\",\"textEN\":\"Mauritius\"},{\"id\":151,\"text\":\"Mauritanie\",\"textEN\":\"Mauritania\"},{\"id\":246,\"text\":\"Mayotte\",\"textEN\":\"Mayotte\"},{\"id\":157,\"text\":\"Mexique\",\"textEN\":\"Mexico\"},{\"id\":73,\"text\":\"Micron\\u00e9sie, \\u00c9tats f\\u00e9d\\u00e9r\\u00e9s de\",\"textEN\":\"Micronesia, Federated States of\"},{\"id\":139,\"text\":\"Moldavie\",\"textEN\":\"Moldova\"},{\"id\":138,\"text\":\"Monaco\",\"textEN\":\"Monaco\"},{\"id\":147,\"text\":\"Mongolie\",\"textEN\":\"Mongolia\"},{\"id\":140,\"text\":\"Mont\\u00e9n\\u00e9gro\",\"textEN\":\"Montenegro\"},{\"id\":152,\"text\":\"Montserrat\",\"textEN\":\"Montserrat\"},{\"id\":159,\"text\":\"Mozambique\",\"textEN\":\"Mozambique\"},{\"id\":146,\"text\":\"Myanmar\",\"textEN\":\"Myanmar\"},{\"id\":160,\"text\":\"Namibie\",\"textEN\":\"Namibia\"},{\"id\":169,\"text\":\"Nauru\",\"textEN\":\"Nauru\"},{\"id\":168,\"text\":\"N\\u00e9pal\",\"textEN\":\"Nepal\"},{\"id\":165,\"text\":\"Nicaragua\",\"textEN\":\"Nicaragua\"},{\"id\":162,\"text\":\"Niger\",\"textEN\":\"Niger\"},{\"id\":164,\"text\":\"Nigeria\",\"textEN\":\"Nigeria\"},{\"id\":170,\"text\":\"Niue\",\"textEN\":\"Niue\"},{\"id\":167,\"text\":\"Norv\\u00e8ge\",\"textEN\":\"Norway\"},{\"id\":161,\"text\":\"Nouvelle-Cal\\u00e9donie\",\"textEN\":\"New Caledonia\"},{\"id\":171,\"text\":\"Nouvelle-Z\\u00e9lande\",\"textEN\":\"New Zealand\"},{\"id\":172,\"text\":\"Oman\",\"textEN\":\"Oman\"},{\"id\":231,\"text\":\"Ouganda\",\"textEN\":\"Uganda\"},{\"id\":235,\"text\":\"Ouzb\\u00e9kistan\",\"textEN\":\"Uzbekistan\"},{\"id\":178,\"text\":\"Pakistan\",\"textEN\":\"Pakistan\"},{\"id\":185,\"text\":\"Palau\",\"textEN\":\"Palau\"},{\"id\":183,\"text\":\"Palestine\",\"textEN\":\"Palestine, State of\"},{\"id\":173,\"text\":\"Panama\",\"textEN\":\"Panama\"},{\"id\":176,\"text\":\"Papouasie-Nouvelle-Guin\\u00e9e\",\"textEN\":\"Papua New Guinea\"},{\"id\":186,\"text\":\"Paraguay\",\"textEN\":\"Paraguay\"},{\"id\":166,\"text\":\"Pays-Bas\",\"textEN\":\"The Netherlands\"},{\"id\":30,\"text\":\"Pays-Bas carib\\u00e9ens\",\"textEN\":\"Caribbean Netherlands \"},{\"id\":174,\"text\":\"P\\u00e9rou\",\"textEN\":\"Peru\"},{\"id\":177,\"text\":\"Philippines\",\"textEN\":\"Philippines\"},{\"id\":181,\"text\":\"Pitcairn\",\"textEN\":\"Pitcairn\"},{\"id\":179,\"text\":\"Pologne\",\"textEN\":\"Poland\"},{\"id\":175,\"text\":\"Polyn\\u00e9sie fran\\u00e7aise\",\"textEN\":\"French Polynesia\"},{\"id\":184,\"text\":\"Portugal\",\"textEN\":\"Portugal\"},{\"id\":182,\"text\":\"Puerto Rico\",\"textEN\":\"Puerto Rico\"},{\"id\":187,\"text\":\"Qatar\",\"textEN\":\"Qatar\"},{\"id\":41,\"text\":\"R\\u00e9publique centrafricaine\",\"textEN\":\"Central African Republic\"},{\"id\":61,\"text\":\"R\\u00e9publique dominicaine\",\"textEN\":\"Dominican Republic\"},{\"id\":56,\"text\":\"R\\u00e9publique tch\\u00e8que\",\"textEN\":\"Czech Republic\"},{\"id\":188,\"text\":\"R\\u00e9union\",\"textEN\":\"R\\u00e9union\"},{\"id\":189,\"text\":\"Roumanie\",\"textEN\":\"Romania\"},{\"id\":77,\"text\":\"Royaume-Uni\",\"textEN\":\"United Kingdom\"},{\"id\":191,\"text\":\"Russie\",\"textEN\":\"Russian Federation\"},{\"id\":192,\"text\":\"Rwanda\",\"textEN\":\"Rwanda\"},{\"id\":66,\"text\":\"Sahara Occidental\",\"textEN\":\"Western Sahara\"},{\"id\":26,\"text\":\"Saint-Barth\\u00e9lemy\",\"textEN\":\"Saint Barth\\u00e9lemy\"},{\"id\":120,\"text\":\"Saint-Kitts-et-Nevis\",\"textEN\":\"Saint Kitts and Nevis\"},{\"id\":204,\"text\":\"Saint-Marin\",\"textEN\":\"San Marino\"},{\"id\":141,\"text\":\"Saint-Martin (France)\",\"textEN\":\"Saint-Martin (France)\"},{\"id\":211,\"text\":\"Saint-Martin (Pays-Bas)\",\"textEN\":\"Sint Maarten (Dutch part)\"},{\"id\":180,\"text\":\"Saint-Pierre-et-Miquelon\",\"textEN\":\"St. Pierre and Miquelon\"},{\"id\":237,\"text\":\"Saint-Vincent-et-les-Grenadines\",\"textEN\":\"Saint Vincent and the Grenadines\"},{\"id\":199,\"text\":\"Sainte-H\\u00e9l\\u00e8ne\",\"textEN\":\"Saint Helena\"},{\"id\":128,\"text\":\"Sainte-Lucie\",\"textEN\":\"Saint Lucia\"},{\"id\":244,\"text\":\"Samoa\",\"textEN\":\"Samoa\"},{\"id\":11,\"text\":\"Samoa am\\u00e9ricaine\",\"textEN\":\"American Samoa\"},{\"id\":209,\"text\":\"Sao Tom\\u00e9-et-Principe\",\"textEN\":\"Sao Tome and Principe\"},{\"id\":205,\"text\":\"S\\u00e9n\\u00e9gal\",\"textEN\":\"Senegal\"},{\"id\":190,\"text\":\"Serbie\",\"textEN\":\"Serbia\"},{\"id\":195,\"text\":\"Seychelles\",\"textEN\":\"Seychelles\"},{\"id\":203,\"text\":\"Sierra Leone\",\"textEN\":\"Sierra Leone\"},{\"id\":198,\"text\":\"Singapour\",\"textEN\":\"Singapore\"},{\"id\":202,\"text\":\"Slovaquie\",\"textEN\":\"Slovakia\"},{\"id\":200,\"text\":\"Slov\\u00e9nie\",\"textEN\":\"Slovenia\"},{\"id\":206,\"text\":\"Somalie\",\"textEN\":\"Somalia\"},{\"id\":196,\"text\":\"Soudan\",\"textEN\":\"Sudan\"},{\"id\":208,\"text\":\"Soudan du Sud\",\"textEN\":\"South Sudan\"},{\"id\":130,\"text\":\"Sri Lanka\",\"textEN\":\"Sri Lanka\"},{\"id\":197,\"text\":\"Su\\u00e8de\",\"textEN\":\"Sweden\"},{\"id\":43,\"text\":\"Suisse\",\"textEN\":\"Switzerland\"},{\"id\":207,\"text\":\"Suriname\",\"textEN\":\"Suriname\"},{\"id\":201,\"text\":\"Svalbard et \\u00eele de Jan Mayen\",\"textEN\":\"Svalbard and Jan Mayen Islands\"},{\"id\":213,\"text\":\"Swaziland\",\"textEN\":\"Swaziland\"},{\"id\":212,\"text\":\"Syrie\",\"textEN\":\"Syria\"},{\"id\":219,\"text\":\"Tadjikistan\",\"textEN\":\"Tajikistan\"},{\"id\":228,\"text\":\"Ta\\u00efwan\",\"textEN\":\"Taiwan\"},{\"id\":229,\"text\":\"Tanzanie\",\"textEN\":\"Tanzania\"},{\"id\":215,\"text\":\"Tchad\",\"textEN\":\"Chad\"},{\"id\":216,\"text\":\"Terres australes fran\\u00e7aises\",\"textEN\":\"French Southern Territories\"},{\"id\":106,\"text\":\"Territoire britannique de l'oc\\u00e9an Indien\",\"textEN\":\"British Indian Ocean Territory\"},{\"id\":218,\"text\":\"Tha\\u00eflande\",\"textEN\":\"Thailand\"},{\"id\":221,\"text\":\"Timor-Leste\",\"textEN\":\"Timor-Leste\"},{\"id\":217,\"text\":\"Togo\",\"textEN\":\"Togo\"},{\"id\":220,\"text\":\"Tokelau\",\"textEN\":\"Tokelau\"},{\"id\":224,\"text\":\"Tonga\",\"textEN\":\"Tonga\"},{\"id\":226,\"text\":\"Trinit\\u00e9-et-Tobago\",\"textEN\":\"Trinidad and Tobago\"},{\"id\":223,\"text\":\"Tunisie\",\"textEN\":\"Tunisia\"},{\"id\":222,\"text\":\"Turkm\\u00e9nistan\",\"textEN\":\"Turkmenistan\"},{\"id\":225,\"text\":\"Turquie\",\"textEN\":\"Turkey\"},{\"id\":227,\"text\":\"Tuvalu\",\"textEN\":\"Tuvalu\"},{\"id\":230,\"text\":\"Ukraine\",\"textEN\":\"Ukraine\"},{\"id\":234,\"text\":\"Uruguay\",\"textEN\":\"Uruguay\"},{\"id\":242,\"text\":\"Vanuatu\",\"textEN\":\"Vanuatu\"},{\"id\":236,\"text\":\"Vatican\",\"textEN\":\"Vatican\"},{\"id\":238,\"text\":\"Venezuela\",\"textEN\":\"Venezuela\"},{\"id\":241,\"text\":\"Vietnam\",\"textEN\":\"Vietnam\"},{\"id\":245,\"text\":\"Y\\u00e9men\",\"textEN\":\"Yemen\"},{\"id\":248,\"text\":\"Zambie\",\"textEN\":\"Zambia\"},{\"id\":249,\"text\":\"Zimbabwe\",\"textEN\":\"Zimbabwe\"}]"

the error i get is :

The JSON value could not be converted to Oaxes.CountryList. Path: $ | LineNumber: 0 | BytePositionInLine: 1.


Thanks for your help and advice
 
Solution
I made it working if someone have the same problem.

I create only this class :

C#:
public class CountryListItem
    {
        public string id { get; set; }
        public string text { get; set; }
        public string textEN { get; set; }
    }

and deserialise like this

C#:
List<CountryListItem> ListOfCountry = JsonConvert.DeserializeObject<List<CountryListItem>>(responseContent);
That's because your class declaration is for a single country, but that JSON response from the web service is for an array or list of countries. It's like trying to catch a single bomblet when you asked for a cluster bomb be sent to your location.
 
That's because your class declaration is for a single country, but that JSON response from the web service is for an array or list of countries. It's like trying to catch a single bomblet when you asked for a cluster bomb be sent to your location.

how ok i see so how i have to declare my class so the deserialise working ?
 
Declare your class to match the JSON.
 
Declare your class to match the JSON.

yes i understand but i already declare it with the 3 value i have for each record of my json : id, text and textEN
data sample :([{\"id\":75,\"text\":\"France\",\"textEN\":\"France\"},{\"id\":3,\"text\":\"Afghanistan\",\"textEN\":\"Afghanistan\"},{\"id\":247,\"text\":\"Afrique du Sud\",\"textEN\":\"South Africa\"},{\"id\":6,\"text\":\"Albanie\",\"textEN\":\"Albania\"},{\"id\":62,\"text\":\"Alg\\u00e9rie\",\"textEN\":\"Algeria\"},{\"id\":57,\"text\": etc...)

C#:
class CountryList
    {
        public string id { get; set; }
        public string text { get; set; }
        public string textEN { get; set; }
    }
 
i try to declare my class with item like this

C#:
public class CountryListItems
    {
        public CountryListItems()
        {
            Events = new List<CountryListItem>();
        }

        public List<CountryListItem> Events { get; set; }
    }

    public class CountryListItem
    {
        public string id { get; set; }
        public string text { get; set; }
        public string textEN { get; set; }
    }

then calling the deserialise like this

C#:
var postResponse = System.Text.Json.JsonSerializer.Deserialize<CountryListItems>(responseContent, options);

but always an error
 
I made it working if someone have the same problem.

I create only this class :

C#:
public class CountryListItem
    {
        public string id { get; set; }
        public string text { get; set; }
        public string textEN { get; set; }
    }

and deserialise like this

C#:
List<CountryListItem> ListOfCountry = JsonConvert.DeserializeObject<List<CountryListItem>>(responseContent);
 
Solution
But that is not what you asked for. You asked for how to change your class. You didn't change your class. You changed the container for your class.

If you had asked how to change your code so that it would deserialize the JSON you were getting, I would have suggested the solution you arrived at, but that is not what you requested.
 
data sample

That's probably not the data as it came back from the API, because it's full of escapes for quotes

The data from the API probably looked like

[{"id":75,"text":"France","textEN":"France"},{"id":3, ...

Be careful not to confuse yourself with what the debugger shows you (strings are encoded so that you could paste them into code as hardcoded values and they would work) versus what the string really looks like (click the magnifying glass next to the string variable tooltip to open the string in the text viewer)

---

I should really make a separate thread about deserialising json and making http calls because I find myself saying it often, but here goes..

i try to declare my class with item like this

It's really hard work to get it right, very boring to create those classes and you've got loads of properties not to naming convention. You can make your life hugely easier by:

1) visit app.QuickType.io
2) paste your JSON into it, pick a root name like CountriesResponse and some other options
3) it generates lovely classes, with type converters, and enum detection, and attributes to keep your naming conventions sane and even one line deserializers that use the type converters (and, and, and..) etc.. it's just beautiful
4) you install a nuget package called Flurl.Http
5) you write some code like:

C#:
//if you want to use quicktype's deser, with the type converters etc
var r = CountriesResponse.FromJson(await "https://YOURHOST.COM/external/api/country".PostJsonAsync(new {} ));

//or if you want it more compact, but not using QT's custom deser code
var r = await "https://YOURHOST.COM/external/api/country".PostJsonAsync<CountriesResponse>(new {} );

Overall this approach will deliver code that is an improvement over what you've got, most notably that Flurl will use HttpClient correctly, whereas you're using it incorrectly. You're also not using await when you should..
 

Latest posts

Back
Top Bottom