1

First: I'm new to using JSON, and I used the answers on here to use Json.Net to deserialize data from a Pokemon API into a C# class (Pokemon class). I used http://json2csharp.com to help me define my class and it looks like this:

public class Pokemon
{
    public Pokemon(string json)
    {         
        JsonConvert.PopulateObject(json, this, PokeApi.JsonSerializerSettings);
    }
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("evolutions")]
    public Evolutions evolutions { get; set; }

    [JsonProperty("national_id")]
    public int national_id { get; set; }
}

with a bunch of other properties like resource_uri, attack stat, etc.

As the answer offered on the aforementioned link said, I used JsonConvert.DeserializeObject(json) like so:

public Pokemon GetPokemon(int nationalId)
{
using (WebClient client = new WebClient())
        {
            var json = client.DownloadString("http://pokeapi.co/api/v1/pokemon/" + nationalId + "/");
            var output = JsonConvert.DeserializeObject<Pokemon>(json);

            return output;
        }
    }

However I keep getting an exception that says "Cannot deserialize the current JSON array (e.g.[1,2,3]) into type 'Evolutions' because the type requires a JSON object..."

I found a lot of other questions asking the same exact thing, but I was confused with the top answers - sometimes the answer was to use JsonProperty, sometimes it was to use JsonConverter, without really an explanation on what all these meant. Do I need both?

Edit: sample json (call: http://pokeapi.co/api/v1/pokemon/1/)

{
  "abilities": [
    {
      "name": "overgrow",
      "resource_uri": "/api/v1/ability/1/"
    },
    {
      "name": "chlorophyll",
      "resource_uri": "/api/v1/ability/2/"
    }
  ],
  "attack": 49,
  "catch_rate": 45,
  "created": "2013-11-02T12:08:25.745455",
  "defense": 49,
  "egg_cycles": 21,
  "egg_groups": [
    {
      "name": "Monster",
      "resource_uri": "/api/v1/egg/1/"
    },
    {
      "name": "Grass",
      "resource_uri": "/api/v1/egg/8/"
    }
  ],
  "ev_yield": "1 Sp Atk",
  "evolutions": {
    "level": 16,
    "method": "level up",
    "resource_uri": "/api/v1/pokemon/2/",
    "to": "Ivysaur"
  },
  "exp": 64,
  "growth_rate": "ms",
  "happiness": 70,
  "height": "2'4",
  "hp": 45,
  "male_female_ratio": "87.5/12.5",
  "modified": "2013-11-02T13:28:04.914889",
  "moves": [
    {
      "learn_type": "other",
      "name": "Tackle",
      "resource_uri": "/api/v1/move/1/"
    },
    {
      "learn_type": "other",
      "name": "Growl",
      "resource_uri": "/api/v1/move/2/"
    },
    {
      "learn_type": "level up",
      "level": 10,
      "name": "Vine whip",
      "resource_uri": "/api/v1/move/3/"
    }
  ],
  "name": "Bulbasaur",
  "national_id": 1,
  "resource_uri": "/api/v1/pokemon/4/",
  "sp_atk": 65,
  "sp_def": 65,
  "species": "seed pokemon",
  "speed": 45,
  "total": 318,
  "types": [
    {
      "name": "grass",
      "resource_uri": "/api/v1/type/5/"
    },
    {
      "name": "poison",
      "resource_uri": "/api/v1/type/8/"
    }
  ],
  "weight": "15.2lbs"
}

Evolutions class:

public class Evolutions
{
    public int level { get; set; }
    public string method { get; set; }
    public string resource_uri { get; set; }
    public string to { get; set; }
}
Community
  • 1
  • 1
user3294629
  • 95
  • 1
  • 7

1 Answers1

0

I tried http://json2csharp.com/ with http://pokeapi.co/api/v1/pokemon/19/ and what I see is

public class RootObject
{
    //...
    public List<Evolution> evolutions { get; set; }
   //...
}

This pretty mush is your Pokemon class. So you need to declare Evolutions as list.

Andrey Stukalin
  • 5,328
  • 2
  • 31
  • 50
  • Huh, I was actually given "public Evolutions evolutions { get; set; }". I declared evolutions as a List and now I get "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentNullException". – user3294629 Feb 18 '14 at 08:17
  • add evolutions = new List() in the constructor – Andrey Stukalin Feb 18 '14 at 08:18
  • 1
    @user3294629 remove *your constructor* – L.B Feb 18 '14 at 08:19
  • @L.B Oh... This worked and now I feel dumb. If it's not too silly a question to ask - how did the presence of the constructor result in that exception? *_* – user3294629 Feb 18 '14 at 08:23
  • @user3294629 it is the *only* constructor of your object, and Json.net uses it to create an instance, but in this case the `json` parameter is null. So you pass null to `JsonConvert.PopulateObject` – L.B Feb 18 '14 at 08:31