从Riot API C#反序列化JSON [英] Deserialize JSON from Riot API C#

查看:142
本文介绍了从Riot API C#反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从C#中的RIOT API反序列化JSON响应时遇到一些问题.我想获取冠军"列表,API返回这样的流:

I have some problem to deserialize JSON response from the RIOT API in C#. I want to get the list of "Champion" and the API return a stream like this :

{  
   "type":"champion",
   "version":"6.1.1",
   "data":{  
      "Thresh":{  
         "id":412,
         "key":"Thresh",
         "name":"Thresh",
         "title":"the Chain Warden"
      },
      "Aatrox":{  
         "id":266,
         "key":"Aatrox",
         "name":"Aatrox",
         "title":"the Darkin Blade"
      },...
    }
}

所有数据都具有相同的属性(id,键,名称和标题),所以我创建了冠军类:

All data has the same attributes (id, key, name and title) so I create a champion class :

public class Champion
    {
        public int id { get; set; }
        public string key { get; set; }
        public string name { get; set; }
        public string title { get; set; }
    }

我需要您的帮助,因为我不知道如何反序列化此数据...我需要创建一个具有类型,版本和数据属性的Root类(数据是冠军列表)?我一直在寻找二手的NewtonSoft Json,但没有找到帮助我的例子.

I need your help because i dont know how to deserialize this data... I need to create a Root class with type, version and data attributes (data is a list of champion)? I watched for used NewtonSoft Json but I dont found example who helped me.

推荐答案

您可以使用以下根对象(更准确地说是数据传输对象)从API检索冠军.这将返回所有冠军,而不必为每个冠军创建一个班级.

You can use the following root object (more accurately Data Transfer Object) to retrieve the champions from the API. This will return all champions without having to create a class for each champion.

public class RootChampionDTO
{
    public string Type { get; set; }
    public string Version { get; set; }
    public Dictionary<string, Champion> Data { get; set; }
}

然后使用Newtsonsoft的Json.NET,使用以下命令反序列化:

then using Newtsonsoft's Json.NET, you would deserialize using the following:

JsonConvert.DeserializeObject<RootChampionDTO>(string json);

这篇关于从Riot API C#反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆