【问题标题】:Parsing JSON from Pokeapi从 Pokeapi 解析 JSON
【发布时间】: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"},...

【问题讨论】:

    标签: c# json api


    【解决方案1】:

    这应该是你的类结构。

    public class Result
    {
        public string url { get; set; }
        public string name { get; set; }
    }
    
    public class Pokemon
    {
        public int count { get; set; }
        public object previous { get; set; }
        public List<Result> results { get; set; }
    }
    

    这必须是有效的 json。

    {
    "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"
    }]
    }
    

    【讨论】:

    • 谢谢。这是有道理的,但我如何避免创建自己的 Pokemon 类而使用 PokeApi.NET 外部库?
    【解决方案2】:

    使用 Newtonsoft 包,您可以像这个示例一样反序列化您的 JSON。还可以使用 json2sharp 工具从您的 JSON 生成 C# 类。

    如果你在 Visual Studio 中有Web Essentials,你可以随时使用

    编辑 > 选择性粘贴 > 将 JSON 粘贴为类。

    public class Result
    {
        public string url { get; set; }
        public string name { get; set; }
    }
    
    public class RootObject
    {
        public int count { get; set; }
        public object previous { get; set; }
        public List<Result> results { get; set; }
    }
    
    var  results = JsonConvert.DeserializeObject<RootObject>(json);
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 2010-12-30
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多