Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:53:46+00:00 2026-05-30T10:53:46+00:00

I am trying to deserialize a json data into a model class but I

  • 0

I am trying to deserialize a json data into a model class but I am failing. Here is what I do:

    public CountryModel GetCountries() {

        using (WebClient client = new WebClient()) {

            var result = client.DownloadString("http://api.worldbank.org/incomeLevels/LIC/countries?format=json");

            var output = JsonConvert.DeserializeObject<List<CountryModel>>(result);

            return output.First();
        }
    }

This is how my model looks like:

public class CountryModel
{
    public int Page { get; set; }
    public int Pages { get; set; }
    public int Per_Page { get; set; }
    public int Total { get; set; }

    public List<Country> Countries { get; set; }
}

public class Country
{
    public int Id { get; set; }
    public string Iso2Code { get; set; }
    public string Name { get; set; }
    public Region Region { get; set; }
}

public class Region
{
    public int Id { get; set; }
    public string Value { get; set; }
}

You can see the Json I am getting here: http://api.worldbank.org/incomeLevels/LIC/countries?format=json

This is the error I get:

Cannot deserialize JSON array into type ‘Mvc4AsyncSample.Models.CountryModel’. Line 1, position 1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:53:48+00:00Added an answer on May 30, 2026 at 10:53 am

    You have to write a custom JsonConverter:

        public class CountryModelConverter : JsonConverter
        {
    
            public override bool CanConvert(Type objectType)
            {
                if (objectType == typeof(CountryModel))
                {
                    return true;
                }
    
                return false;
            }
    
            public override object ReadJson(JsonReader reader, Type objectType
                , object existingValue, JsonSerializer serializer)
            {
                reader.Read(); //start array
                //reader.Read(); //start object
                JObject obj = (JObject)serializer.Deserialize(reader);
    
                //{"page":1,"pages":1,"per_page":"50","total":35}
                var model = new CountryModel();
    
                model.Page = Convert.ToInt32(((JValue)obj["page"]).Value);
                model.Pages = Convert.ToInt32(((JValue)obj["pages"]).Value);
                model.Per_Page = Int32.Parse((string) ((JValue)obj["per_page"]).Value);
                model.Total = Convert.ToInt32(((JValue)obj["total"]).Value);
    
                reader.Read(); //end object
    
                model.Countries = serializer.Deserialize<List<Country>>(reader);
    
                reader.Read(); //end array
    
                return model;
            }
    
            public override void WriteJson(JsonWriter writer, object value
                , JsonSerializer serializer)
            {
                throw new NotImplementedException();
            }
        }
    

    And tag the CountryModel with that converter (I also had to switch some int to string):

        [JsonConverter(typeof(CountryModelConverter))]
        public class CountryModel
        {
            public int Page { get; set; }
            public int Pages { get; set; }
            public int Per_Page { get; set; }
            public int Total { get; set; }
    
            public List<Country> Countries { get; set; }
        }
    
        public class Country
        {
            public string Id { get; set; }
            public string Iso2Code { get; set; }
            public string Name { get; set; }
            public Region Region { get; set; }
        }
    
        public class Region
        {
            public string Id { get; set; }
            public string Value { get; set; }
        }
    

    Then you should be able to deserialize like this:

    var output = JsonConvert.DeserializeObject<CountryModel>(result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are some models I am trying to load data for: class School(models.Model): name
I'm trying to deserialize some JSON data into objects for an application. Up until
I'm trying to deserialize json to an object model where the collections are represented
I am trying to parse out data from oodle.com api feed using the JSON.NET
I'm trying to deseralize some json into a collection (list), but I'm not sure
I'm trying to use Jackson to convert some JSON data into Java objects ,a
I am trying to deserialize/map the below JSON to List<Bill > java object using
I am trying to write a generic method for deserializing json into my model.
I'm trying to grab some JSON data from a web service using JSON.Net. The
I am trying to deserialize a JSON string using VB.net and cannot seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.