简体   繁体   English

从Riot API C#反序列化JSON

[英]Deserialize JSON from Riot API C#

I have some problem to deserialize JSON response from the RIOT API in C#. 我在从C#中的RIOT API反序列化JSON响应时遇到一些问题。 I want to get the list of "Champion" and the API return a stream like this : 我想获取“冠军”列表,API返回这样的流:

{  
   "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"
      },...
    }
}

All data has the same attributes (id, key, name and title) so I create a champion class : 所有数据都具有相同的属性(id,键,名称和标题),因此我创建了Champion类:

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

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)? 我需要您的帮助,因为我不知道如何反序列化此数据...我需要创建一个具有类型,版本和数据属性的Root类(数据是冠军列表)? I watched for used NewtonSoft Json but I dont found example who helped me. 我看着二手的NewtonSoft Json,但我找不到帮助我的例子。

You can use the following root object (more accurately Data Transfer Object) to retrieve the champions from the API. 您可以使用以下根对象(更准确地说是数据传输对象)从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; }
}

then using Newtsonsoft's Json.NET, you would deserialize using the following: 然后使用Newtsonsoft的Json.NET,可以使用以下方法反序列化:

JsonConvert.DeserializeObject<RootChampionDTO>(string json);

If you want to use NewtonSoft: 如果要使用NewtonSoft:

JsonConvert.DeserializeObject<RootObject>(string json);

Json .NET Documentation: http://www.newtonsoft.com/json/help/html/SerializingJSON.htm Json .NET文档: http : //www.newtonsoft.com/json/help/html/SerializingJSON.htm

Consider such classes: 考虑以下类:

public class ResponseModel
{
    public string Type { get; set; }

    public string Version { get; set; }

    public Dictionary<string, Champion> Data { get; set; }
}

public class Champion
{
    public int Id { get; set; }

    public string Key { get; set; }

    public string Name { get; set; }

    public string Title { get; set; }
}

And after use Newtonsoft.Json nuget package to deserialize your json: 然后使用Newtonsoft.Json nuget包反序列化您的json:

 using Newtonsoft.Json;

 var result = JsonConvert.DeserializeObject<ResponseModel>(json);

Note that Newtonsoft.Json default settings allow you to correctly parse camelCase properties from json into PascalCase properties in C# classes. 请注意,Newtonsoft.Json的默认设置允许您将json中的camelCase属性正确地解析为C#类中的PascalCase属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM