1

首先:我是使用 JSON 的新手,我使用此处的答案使用 Json.Net 将来自 Pokemon API 的数据反序列化为 C# 类(Pokemon 类)。我使用http://json2csharp.com来帮助我定义我的类,它看起来像这样:

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; }
}

具有许多其他属性,例如 resource_uri、攻击统计等。

正如上述链接上提供的答案所说,我像这样使用 JsonConvert.DeserializeObject(json) :

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;
        }
    }

但是,我不断收到一个异常,上面写着“无法将当前的 JSON 数组(例如 [1,2,3])反序列化为 'Evolutions' 类型,因为该类型需要 JSON 对象......”

我发现很多其他问题都在问同样的问题,但我对最重要的答案感到困惑——有时答案是使用 JsonProperty,有时是使用 JsonConverter,但没有真正解释所有这些意味着什么。我需要两者吗?

编辑:示例 json(调用: 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"
}

进化类:

public class Evolutions
{
    public int level { get; set; }
    public string method { get; set; }
    public string resource_uri { get; set; }
    public string to { get; set; }
}
4

1 回答 1

0

我用http://pokeapi.co/api/v1/pokemon/19/尝试了http://json2csharp.com/ ,我看到的是

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

这个漂亮的糊状物是你的口袋妖怪课。所以你需要将 Evolutions 声明为列表。

于 2014-02-18T08:13:22.770 回答