如何在c#中将JSONObject反序列化为JSONArray

本文关键字:反序列化 JSONArray JSONObject 中将 | 更新日期: 2023-09-27 18:12:14

我正试图从我无法控制的web服务器反序列化JSON响应。如下所示,结果对象作为JSONObject返回。这里的问题在于每个对象的钥匙对每个玩家来说都是独一无二的。据我所知,我需要为每个唯一的JSONObject创建一个单独的类来正确地反序列化字符串,考虑到每个对象的键对于从web服务器返回的每个创建的帐户都是唯一的,这根本行不通。我想知道是否有可能将JSONObject"播放器"反序列化为JSONArray,或者我是否想得太多了。列表中返回的球员数量也不同,最多为32个。

{
"players": {
  "930153636": {
    "name": "ur_moms_cookies",
    "tag": "",
    "rank": 62,
    "score": 1332,
    "kills": 9,
    "deaths": 2,
    "squad": 1,
    "role": 1
  },
  "250721735": {
    "name": "PyRobotic",
    "tag": "",
    "rank": 5,
    "score": 1786,
    "kills": 8,
    "deaths": 4,
    "squad": 2,
    "role": 1
  },
  "1035406266": {
    "name": "ShrapnalFire",
    "tag": "GaGu",
    "rank": 60,
    "score": 1455,
    "kills": 8,
    "deaths": 7,
    "squad": 1,
    "role": 1
  },
  "239767512": {
    "name": "NeoEllis",
    "tag": "",
    "rank": 77,
    "score": 3244,
    "kills": 20,
    "deaths": 6,
    "squad": 3,
    "role": 1
  },
  "936400786": {
    "name": "antiares51",
    "tag": "",
    "rank": 140,
    "score": 4720,
    "kills": 17,
    "deaths": 2,
    "squad": 2,
    "role": 1
  },
  "355364783": {
    "name": "BombKat11",
    "tag": "",
    "rank": 16,
    "score": 1347,
    "kills": 9,
    "deaths": 5,
    "squad": 4,
    "role": 1
  },
  "803046096": {
    "name": "thejoedude",
    "tag": "FSJ",
    "rank": 66,
    "score": 1294,
    "kills": 8,
    "deaths": 8,
    "squad": 4,
    "role": 1
  },
  "788221305": {
    "name": "Tiyou33",
    "tag": "",
    "rank": 45,
    "score": 1927,
    "kills": 4,
    "deaths": 5,
    "squad": 2,
    "role": 1
  },
  "353394766": {
    "name": "DKOfTFC",
    "tag": "RSPN",
    "rank": 119,
    "score": 1895,
    "kills": 9,
    "deaths": 4,
    "squad": 4,
    "role": 1
  },
  "370847289": {
    "name": "shohet10",
    "tag": "",
    "rank": 40,
    "score": 1570,
    "kills": 7,
    "deaths": 7,
    "squad": 6,
    "role": 1
  },
  "206872615": {
    "name": "me94132",
    "tag": "",
    "rank": 60,
    "score": 2045,
    "kills": 7,
    "deaths": 4,
    "squad": 3,
    "role": 1
  },
  "332317870": {
    "name": "frehgv",
    "tag": "",
    "rank": 48,
    "score": 713,
    "kills": 3,
    "deaths": 4,
    "squad": 4,
    "role": 1
  },
  "1144758913": {
    "name": "MudCruncher_78",
    "tag": "",
    "rank": 61,
    "score": 1822,
    "kills": 7,
    "deaths": 5,
    "squad": 6,
    "role": 1
  },
  "603412175": {
    "name": "UsedTissue",
    "tag": "",
    "rank": 119,
    "score": 1551,
    "kills": 10,
    "deaths": 1,
    "squad": 1,
    "role": 1
  },
  "1655466910": {
    "name": "JackTheRipp3r2",
    "tag": "",
    "rank": 27,
    "score": 797,
    "kills": 2,
    "deaths": 3,
    "squad": 8,
    "role": 1
  },
  "869745436": {
    "name": "Negibou",
    "tag": "",
    "rank": 139,
    "score": 4792,
    "kills": 14,
    "deaths": 4,
    "squad": 2,
    "role": 1
  },
  "447652633": {
    "name": "RufusTheRabbit",
    "tag": "",
    "rank": 91,
    "score": 1468,
    "kills": 7,
    "deaths": 4,
    "squad": 4,
    "role": 1
  },
  "352650707": {
    "name": "DJ1101",
    "tag": "",
    "rank": 28,
    "score": 847,
    "kills": 5,
    "deaths": 6,
    "squad": 3,
    "role": 1
  },
  "855246326": {
    "name": "-Frigadier-",
    "tag": "",
    "rank": 109,
    "score": 667,
    "kills": 1,
    "deaths": 6,
    "squad": 1,
    "role": 1
  },
  "255855183": {
    "name": "Asakajim",
    "tag": "",
    "rank": 6,
    "score": 514,
    "kills": 0,
    "deaths": 8,
    "squad": 8,
    "role": 1
  },
  "248119620": {
    "name": "Kokovec",
    "tag": "FCM",
    "rank": 121,
    "score": 891,
    "kills": 5,
    "deaths": 3,
    "squad": 8,
    "role": 1
  },
  "348016421": {
    "name": "pandaymd",
    "tag": "",
    "rank": 122,
    "score": 1595,
    "kills": 6,
    "deaths": 9,
    "squad": 2,
    "role": 1
  },
  "250545778": {
    "name": "Vulkkann",
    "tag": "",
    "rank": 40,
    "score": 2323,
    "kills": 14,
    "deaths": 8,
    "squad": 3,
    "role": 1
  },
  "949941984": {
    "name": "WarchiefBigzb",
    "tag": "",
    "rank": 70,
    "score": 1013,
    "kills": 5,
    "deaths": 3,
    "squad": 9,
    "role": 1
  },
  "268773188": {
    "name": "Brutuka",
    "tag": "",
    "rank": 48,
    "score": 952,
    "kills": 5,
    "deaths": 4,
    "squad": 5,
    "role": 1
  },
  "801259600": {
    "name": "gpc4567",
    "tag": "",
    "rank": 109,
    "score": 1710,
    "kills": 11,
    "deaths": 5,
    "squad": 3,
    "role": 1
  },
  "338093557": {
    "name": "Zelios86",
    "tag": "",
    "rank": 140,
    "score": 1400,
    "kills": 7,
    "deaths": 4,
    "squad": 8,
    "role": 1
  },
  "364705773": {
    "name": "Fleischwolfx",
    "tag": "",
    "rank": 121,
    "score": 1863,
    "kills": 13,
    "deaths": 2,
    "squad": 5,
    "role": 1
  },
  "173524306": {
    "name": "BlackDynamite901",
    "tag": "",
    "rank": 75,
    "score": 583,
    "kills": 4,
    "deaths": 2,
    "squad": 9,
    "role": 1
  },
  "339527854": {
    "name": "Xixus",
    "tag": "",
    "rank": 21,
    "score": 0,
    "kills": 0,
    "deaths": 0,
    "squad": 1,
    "role": 1
  },
  "1667184018": {
    "name": "BckBone71",
    "tag": "",
    "rank": 35,
    "score": 98,
    "kills": 1,
    "deaths": 1,
    "squad": 5,
    "role": 1
  }
 }
}

到目前为止,我已经尝试了一些变化沿着下面的代码片段,当然不工作,因为播放器对象不是一个数组。

public class Team
{
    public int faction;

    [JsonProperty("players")]
    public List<Player> players { get; set; }
}
public class Player
{
    public string name;
    public string tag;
    public int rank;
}

如何在c#中将JSONObject反序列化为JSONArray

试试这样:

var players = new List<Player>();
dynamic dObject = JObject.Parse(JSON);
foreach (var property in dObject.players) {
    var player = property.Value;
    var playerModel = new Player {
        Name = player.name,
        Tag = player.tag,
        Rank = player.rank
    };
    players.Add(playerModel);
}

其中JSON是您的Json对象。在这个例子中,我只是让它成为一个字符串来测试你的对象。这是用NewtonSoft。Json用于解析。

一旦你得到了播放器,你应该可以访问它的所有属性,而不必担心唯一的播放器ID。

这里是.net提琴:https://dotnetfiddle.net/9iOT5w.