Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将包含多个对象的json对象反序列化为列表_C#_Json_Api_Json Deserialization - Fatal编程技术网

C# 将包含多个对象的json对象反序列化为列表

C# 将包含多个对象的json对象反序列化为列表,c#,json,api,json-deserialization,C#,Json,Api,Json Deserialization,我正在尝试与传奇联盟api合作,从[1]获得冠军名单: 这将返回类似json的结果 { "type": "champion", "format": "standAloneComplex", "version": "6.24.1", "data": { "Aatrox": { "version": "6.24.1", "id": "Aatrox", "key": "266", "name": "Aatrox",

我正在尝试与传奇联盟api合作,从[1]获得冠军名单:

这将返回类似json的结果

    {
  "type": "champion",
  "format": "standAloneComplex",
  "version": "6.24.1",
  "data": {
    "Aatrox": {
      "version": "6.24.1",
      "id": "Aatrox",
      "key": "266",
      "name": "Aatrox",
      "title": "the Darkin Blade",
      "blurb": "Aatrox is a legendary warrior, one of only five that remain of an ancient race known as the Darkin. He wields his massive blade with grace and poise, slicing through legions in a style that is hypnotic to behold. With each foe felled, Aatrox's ...",
      "info": {
        "attack": 8,
        "defense": 4,
        "magic": 3,
        "difficulty": 4
      },
      "image": {
        "full": "Aatrox.png",
        "sprite": "champion0.png",
        "group": "champion",
        "x": 0,
        "y": 0,
        "w": 48,
        "h": 48
      },
      "tags": [
        "Fighter",
        "Tank"
      ],
      "partype": "BloodWell",
      "stats": {
        "hp": 537.8,
        "hpperlevel": 85,
        "mp": 105.6,
        "mpperlevel": 45,
        "movespeed": 345,
        "armor": 24.384,
        "armorperlevel": 3.8,
        "spellblock": 32.1,
        "spellblockperlevel": 1.25,
        "attackrange": 150,
        "hpregen": 6.59,
        "hpregenperlevel": 0.5,
        "mpregen": 0,
        "mpregenperlevel": 0,
        "crit": 0,
        "critperlevel": 0,
        "attackdamage": 60.376,
        "attackdamageperlevel": 3.2,
        "attackspeedoffset": -0.04,
        "attackspeedperlevel": 3
      }
    },
    "Ahri": {
      "version": "6.24.1",
      "id": "Ahri",
      "key": "103",
      "name": "Ahri",
      "title": "the Nine-Tailed Fox",
      "blurb": "Unlike other foxes that roamed the woods of southern Ionia, Ahri had always felt a strange connection to the magical world around her; a connection that was somehow incomplete. Deep inside, she felt the skin she had been born into was an ill fit for ...",
      "info": {
        "attack": 3,
        "defense": 4,
        "magic": 8,
        "difficulty": 5
      },
      "image": {
        "full": "Ahri.png",
        "sprite": "champion0.png",
        "group": "champion",
        "x": 48,
        "y": 0,
        "w": 48,
        "h": 48
      },
      "tags": [
        "Mage",
        "Assassin"
      ],
      "partype": "MP",
      "stats": {
        "hp": 514.4,
        "hpperlevel": 80,
        "mp": 334,
        "mpperlevel": 50,
        "movespeed": 330,
        "armor": 20.88,
        "armorperlevel": 3.5,
        "spellblock": 30,
        "spellblockperlevel": 0,
        "attackrange": 550,
        "hpregen": 6.505,
        "hpregenperlevel": 0.6,
        "mpregen": 6,
        "mpregenperlevel": 0.8,
        "crit": 0,
        "critperlevel": 0,
        "attackdamage": 53.04,
        "attackdamageperlevel": 3,
        "attackspeedoffset": -0.065,
        "attackspeedperlevel": 2
      }
    },
    "Akali": {
      "version": "6.24.1",
      "id": "Akali",
      "key": "84",
      "name": "Akali",
      "title": "the Fist of Shadow",
      "blurb": "There exists an ancient order originating in the Ionian Isles dedicated to the preservation of balance. Order, chaos, light, darkness -- all things must exist in perfect harmony for such is the way of the universe. This order is known as the Kinkou ...",
      "info": {
        "attack": 5,
        "defense": 3,
        "magic": 8,
        "difficulty": 7
      },
      "image": {
        "full": "Akali.png",
        "sprite": "champion0.png",
        "group": "champion",
        "x": 96,
        "y": 0,
        "w": 48,
        "h": 48
      },
      "tags": [
        "Assassin"
      ],
      "partype": "Energy",
      "stats": {
        "hp": 587.8,
        "hpperlevel": 85,
        "mp": 200,
        "mpperlevel": 0,
        "movespeed": 350,
        "armor": 26.38,
        "armorperlevel": 3.5,
        "spellblock": 32.1,
        "spellblockperlevel": 1.25,
        "attackrange": 125,
        "hpregen": 8.34,
        "hpregenperlevel": 0.65,
        "mpregen": 50,
        "mpregenperlevel": 0,
        "crit": 0,
        "critperlevel": 0,
        "attackdamage": 58.376,
        "attackdamageperlevel": 3.2,
        "attackspeedoffset": -0.1,
        "attackspeedperlevel": 3.1
      }
    },
...
我应该能够像这样反序列化:

 class ChampionService : BaseService
{
    public List<Champion> GetChampions()
    {
        HttpWebRequest req = CreateRequest(ApiUrl);
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        string json;

        using (StreamReader reader = new StreamReader(res.GetResponseStream()))
        {
            json = reader.ReadToEnd();
            return JsonConvert.DeserializeObject<List<Champion>>(json);
        }
    }
}
class ChampionService:BaseService
{
公共列表GetChampions()
{
HttpWebRequest req=CreateRequest(ApiUrl);
HttpWebResponse res=(HttpWebResponse)req.GetResponse();
字符串json;
使用(StreamReader=newstreamreader(res.GetResponseStream()))
{
json=reader.ReadToEnd();
返回JsonConvert.DeserializeObject(json);
}
}
}
如果数据是数组,我的冠军级:

public class Champion
{
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("key")]
    public int Key { get; set; }
    [JsonProperty("tags")]
    public List<string> tags { get; set; }
}
公开课冠军
{
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“密钥”)]
公共int密钥{get;set;}
[JsonProperty(“标记”)]
公共列表标记{get;set;}
}

但是,由于api返回包含更多对象的数据对象,因此不能将其视为数组,因此无法工作。有没有一个好的方法来实现这一点?

您可以通过使用一个在champions dictionary中占据一席之地的类来对JSON进行不同的建模:

class ApiResult
{
    [JsonProperty("data")]
    public Dictionary<string, Champion> Champions { get; set; }
}
类ApiResult
{
[JsonProperty(“数据”)]
公共字典{get;set;}
}
然后要反序列化,您可以执行以下操作:

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

稍后,您可以使用champions dictionary上的ToList()方法检索Champion列表。

您可以使用一个作为champions dictionary占位符的类来对JSON进行不同的建模:

class ApiResult
{
    [JsonProperty("data")]
    public Dictionary<string, Champion> Champions { get; set; }
}
类ApiResult
{
[JsonProperty(“数据”)]
公共字典{get;set;}
}
然后要反序列化,您可以执行以下操作:

var result = JsonConvert.DeserializeObject<ApiResult>(json);
var result=JsonConvert.DeserializeObject(json);
稍后,您可以使用champions dictionary上的ToList()方法检索Champion列表。

参考我的答案,从您拥有的JSON创建一个类。然后反序列化到该类。参考我的答案,从您拥有的JSON创建一个类。然后反序列化到该类。