简体   繁体   中英

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. 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. Nested objects, such as roster and its contained playersX objects are themselves represented as dictionaries.

So, for example, you would get to the name property of the player1 object like so:

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

If it helps at all, you could just make the roster property dynamic , like so:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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