【发布时间】:2016-10-16 11:51:45
【问题描述】:
我正在尝试了解如何调用 API (Pokeapi) 并将响应中收到的数据解析为 C# 中外部库 (PokeApi.NET) 中的对象。
我在 C# 代码中收到了响应,但我在将响应解析为对象时遇到了困难。
我认为问题出在最后一行代码,但我不确定应该是什么?
C#代码:
private static string URL = "http://pokeapi.co/api/v2/pokemon/";
static async void GetPokemonAsync()
{
string page = URL;
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(page))
using (HttpContent content = response.Content)
{
string result = await content.ReadAsStringAsync();
if (result != null && result.Length >= 50)
{
Console.WriteLine(result.Substring(0,200));
PokeAPI.Pokemon a = JsonConvert.DeserializeObject<PokeAPI.Pokemon>(result);
}
}
}
结果示例:
{"count":811,"previous":null,"results":[{"url":"http:\/\/pokeapi.co\/api\/v2\/pokemon\/1\/","name":"bulbasaur"},{"url":"http:\/\/pokeapi.co\/api\/v2\/pokemon\/2\/","name":"ivysaur"},{"url":"http:\/\/pokeapi.co\/api\/v2\/pokemon\/3\/","name":"venusaur"},{"url":"http:\/\/pokeapi.co\/api\/v2\/pokemon\/4\/","name":"charmander"},{"url":"http:\/\/pokeapi.co\/api\/v2\/pokemon\/5\/","name":"charmeleon"},{"url":"http:\/\/pokeapi.co\/api\/v2\/pokemon\/6\/","name":"charizard"},...
【问题讨论】: