简体   繁体   English

使用.NET deserialize()和嵌套的“列表”进行JSON解析

[英]JSON parsing using .NET deserialize() with nested “list”

EDIT: Simplified the classes 编辑:简化类

{
  "name": "Final Five",
  "bio": null,
  "noPlayers": "0",
  "roster": {
    "players0": {
      "playerId": "3516",
      "name": "Gate",
      "role": "Mid Lane",
      "isStarter": 1
    },
    "players1": {
      "playerId": "3826",
      "name": "Veritas",
      "role": "AD Carry",
      "isStarter": 1
    },
    "players2": {
      "playerId": "4054",
      "name": "Novel",
      "role": "Support",
      "isStarter": 1
    },
    "players3": {
      "playerId": "4142",
      "name": "Wizardry",
      "role": "Top Lane",
      "isStarter": 0
    },
    "players4": {
      "playerId": "4555",
      "name": "Metroid",
      "role": "Jungler",
      "isStarter": 0
    },
    "players5": {
      "playerId": "4554",
      "name": "Chau",
      "role": "Jungler",
      "isStarter": 0
    },
    "players6": {
      "playerId": "3847",
      "name": "Biofrost",
      "role": "Support",
      "isStarter": 0
    }
  },
  "logoUrl": "http://riot-web-cdn.s3-us-west-1.amazonaws.com/lolesports/s3fs-public/final-five-logo.png",
  "profileUrl": "http://na.lolesports.com/node/3498",
  "teamPhotoUrl": "http://na.lolesports.com/",
  "acronym": "F5"
}

I have this json being received on my end. 我收到了这个json消息。 The problem I'm having is trying to parse the players as a list instead of individual elements since the number of players may vary. 我遇到的问题是尝试将播放器解析为列表而不是单个元素,因为播放器的数量可能会有所不同。 I've tried using arrays and lists. 我试过使用数组和列表。 These are the classes I have set up 这些是我设置的课程

public class Player
    {
        public string playerId { get; set; }
        public string name { get; set; }
        public string role { get; set; }
        public int isStarter { get; set; }
    }

    public class Roster
    {
        public Player players0 { get; set; }
        public Player players1 { get; set; }
        public Player players2 { get; set; }
        public Player players3 { get; set; }
        public Player players4 { get; set; }
        public Player players5 { get; set; }
        public Player players6 { get; set; }
    }

    public class Team
    {
        public string name { get; set; }
        public object bio { get; set; }
        public string noPlayers { get; set; }
        public Roster roster { get; set; }
        public string logoUrl { get; set; }
        public string profileUrl { get; set; }
        public string teamPhotoUrl { get; set; }
        public string acronym { get; set; }
    }

This is my deserialization: 这是我的反序列化:

 Team team = new JavaScriptSerializer().Deserialize<Team>(responseText);

One possibility is to deserialize into a dynamic object and convert from that to your strongly-typed object. 一种可能是反序列化为动态对象,然后将其从其转换为强类型对象。 Like so: 像这样:

var dict = new JavaScriptSerializer().Deserialize<dynamic>(responseText);

The resulting dict object is a dictionary, with each property represented as a name-value pair in the dictionary. 生成的dict对象是一个字典,每个属性在字典中均表示为“名称/值”对。 Nested objects, such as roster and its contained playersX objects are themselves represented as dictionaries. 嵌套对象(例如roster及其包含的playersX对象)本身表示为词典。

So, for example, you would get to the name property of the player1 object like so: 因此,例如,您将获得player1对象的name属性,如下player1

Assert.AreEqual("Veritas", dict["roster"]["players1"]["name"]);

If it helps at all, you could just make the roster property dynamic , like so: 如果有帮助的话,您可以将roster属性roster dynamic ,如下所示:

public class Team
{
    public string name { get; set; }
    public object bio { get; set; }
    public string noPlayers { get; set; }
    public dynamic roster { get; set; }
    public string logoUrl { get; set; }
    public string profileUrl { get; set; }
    public string teamPhotoUrl { get; set; }
    public string acronym { get; set; }
}

Then only that property will be deserialized as a dictionary of dictionaries. 然后,只有该属性才能反序列化为字典。

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

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