我试图了解如何调用API(Pokeapi)并将响应中收到的数据解析为C#中外部库(PokeApi.NET)中的对象。解析来自Pokeapi的JSON
我在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"},...
谢谢。这是有道理的,但我如何避免创建我自己的口袋妖怪类,而是使用PokeApi.NET外部库? – Bhav