[英]Cannot deserialize the current JSON array
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). 首先:我是使用JSON的新手,我在这里使用了答案,使用Json.Net将来自Pokemon API的数据反序列化为C#类(Pokemon类)。 I used http://json2csharp.com to help me define my class and it looks like this: 我使用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; }
}
with a bunch of other properties like resource_uri, attack stat, etc. 以及一堆其他属性,例如resource_uri,攻击统计信息等。
As the answer offered on the aforementioned link said, I used JsonConvert.DeserializeObject(json) like so: 正如上述链接提供的答案所说,我像这样使用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;
}
}
However I keep getting an exception that says "Cannot deserialize the current JSON array (eg[1,2,3]) into type 'Evolutions' because the type requires a JSON object..." 但是,我不断收到一个异常,说“无法将当前JSON数组(例如[1,2,3])反序列化为类型'Evolutions',因为该类型需要JSON对象...”
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. 我发现很多其他问题都在问同样的问题,但我对最重要的答案感到困惑-有时答案是使用JsonProperty,有时是使用JsonConverter,而没有真正解释所有这些含义。 Do I need both? 我两个都需要吗?
Edit: sample json (call: http://pokeapi.co/api/v1/pokemon/1/ ) 编辑:样本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"
}
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; }
}
I tried http://json2csharp.com/ with http://pokeapi.co/api/v1/pokemon/19/ and what I see is 我尝试使用http://pokeapi.co/api/v1/pokemon/19/尝试http://json2csharp.com/ ,我看到的是
public class RootObject
{
//...
public List<Evolution> evolutions { get; set; }
//...
}
This pretty mush is your Pokemon class. 这个漂亮的糊糊是您的神奇宝贝课。 So you need to declare Evolutions as list. 因此,您需要将Evolutions声明为列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.