-1

I am trying to deserialize a json object that contains a list of countries but I Keep getting an error that reads

Type System.String' is not supported for deserialization of an array.

I am able to retrieve the JSON object(List of Countries) from the API but when I try to deserialize the JSON object

This is my method to get the list of countries and tie them to a countriesList model

public List<CountriesList> GetCountries()
            {
                try
                {                     
                    string apiCountriesUrl = "https://restcountries.eu/rest/v2/all"; 
                    string response = GetServiceCallByUrl(apiCountriesUrl);
                    var countriesObj = System.Web.Helpers.Json.Decode<List<CountriesList>>(response);

                    return countriesObj;
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }

My CountriesList Model

  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

namespace ApplicationPortal.Models
{
    public class CountriesList
    {
        public string name { get; set; }
        public string callingCodes { get; set; }
    }
}
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • 1
    I know adding the word correctly twice in a post title doesn't help it correct itself. –  May 09 '20 at 15:51
  • Does this answer your question? [How do I deserialize a complex JSON object in C# .NET?](https://stackoverflow.com/questions/16339167/how-do-i-deserialize-a-complex-json-object-in-c-sharp-net) – Pavel Anikhouski May 09 '20 at 15:59

4 Answers4

0

callingCodes is an array of strings, change your model to public List<string> callingCodes { get; set; }

ScottyD0nt
  • 348
  • 2
  • 8
0

dotnetfiddle

see the code above.

I used quicktype.io to get the POCO class for the json that comes from the the api call

The error you get is because callingCodes is a list of string and in your model its a string

public List<string> CallingCodes { get; set; }

update your model so that it reflects the json model

or use a model generator like the one I used.

My code uses Newtonsoft.Json package as quicktype.io uses that, you can modify it and keep your model limited only to the fields you need and you should be able to use System.Web.Helpers.Json.Decode as you are using right now.

Shehroze Malik
  • 111
  • 1
  • 10
-1

You need to add Newtonsoft.dll reference before trying this:

public List<CountriesList> GetCountries()
            {
                try
                {                     
                    string apiCountriesUrl = "https://restcountries.eu/rest/v2/all"; 
                    string response = GetServiceCallByUrl(apiCountriesUrl);
                    var countriesObj = JsonConvert.DeserializeObject<List<CountriesList>>(response);

                    return countriesObj;
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
Rithik Banerjee
  • 447
  • 4
  • 16
-1

I would recommend auto generating the classes required from your data:

Copy an example of the json you wish to import, then in Visual Studio, chose Edit/Paste Special/Paste classes as Json.

This will produce classes that allow the json to be imported correctly, without any guessing.

There are online versions that produce even better code: https://app.quicktype.io/?l=csharp

Neil
  • 11,059
  • 3
  • 31
  • 56