使用带有嵌套“list”

时间:2015-06-19 20:12:30

标签: javascript c# json parsing serialization

编辑:简化课程

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

我收到了这个json。我遇到的问题是尝试将玩家解析为列表而不是单个元素,因为玩家的数量可能会有所不同。我尝试过使用数组和列表。这些是我设置的课程

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; }
    }

这是我的反序列化:

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

1 个答案:

答案 0 :(得分:0)

一种可能性是反序列化为动态对象并将其转换为强类型对象。像这样:

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

生成的dict对象是一个字典,每个属性都表示为字典中的名称 - 值对。嵌套对象(例如roster及其包含的playersX对象本身表示为字典。

因此,举例来说,您可以像name那样访问player1对象的Assert.AreEqual("Veritas", dict["roster"]["players1"]["name"]); 属性:

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; } } 那样:

validaHoraNumero: function(iContTabla,iContFila){

    var self = this;

    $("#hora_"+iContTabla+"_"+iContFila+"_id").keydown(function (e) { 
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 || $(this).val().indexOf('.') != -1 || (e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) || (e.keyCode >= 35 && e.keyCode <= 40)) {
            return;
        }
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
},

然后只将该属性反序列化为字典词典。