【问题标题】:C# JSON.net -Array like- Object Deserialization of non-constant name objectsC# JSON.net -Array like- 非常量名称对象的对象反序列化
【发布时间】:2014-03-22 19:36:52
【问题描述】:

我正在尝试反序列化下面的 json:

{
    "raw": {
        "435": {
            "clubId": 435,
            "name": "Last Gladiators",
            "seasons": 154,
            "titlesWon": "21",
            "leaguesWon": "0",
            "divsWon1": 10,
            "divsWon2": -4,
            "divsWon3": 6,
            "divsWon4": 10,
            "cupsWon1": "1",
            "cupsWon2": "1",
            "cupsWon3": "1",
            "cupsWon4": "0",
            "dblsWon1": "1",
            "dblsWon2": "1",
            "dblsWon3": "1",
            "dblsWon4": "0",
            "cupsElim1": "1",
            "cupsElim2": "7",
            "cupsElim3": "25",
            "cupsElim4": "0",
            "promotions": "47",
            "holds": "66",
            "relegations": "41",
            "currentPoints": "9329",
            "currentDivision": "7",
            "prevDivision": "6",
            "maxDivision": "10",
            "bestDivision": 1,
            "bestPoints": "4",
            "curSeasonMov": "-2",
            "lastMatch0": "2",
            "lastMatch1": "0",
            "lastMatch2": "1",
            "lastMatch3": "0",
            "lastMatch4": "2",
            "lastMatch5": "2",
            "lastOpponent0": "34703",
            "lastOpponent1": "69960",
            "lastOpponent2": "781",
            "lastOpponent3": "57091",
            "lastOpponent4": "70263",
            "lastOpponent5": "59048",
            "starLevel": "10",
            "cupRankingPoints": "324",
            "overallRankingPoints": "9653",
            "alltimeGoals": "2141",
            "alltimeGoalsAgainst": "2111",
            "seasonWins": "3",
            "seasonTies": "2",
            "seasonLosses": "2",
            "gamesPlayed": "7",
            "goals": "16",
            "goalsAgainst": "10",
            "points": "11",
            "projectedPoints": "15",
            "prevSeasonWins": "2",
            "prevSeasonTies": "2",
            "prevSeasonLosses": "2",
            "prevPoints": "8",
            "prevProjectedPts": "13",
            "skill": "32",
            "wins": "548",
            "ties": "301",
            "losses": "606"
        },
        "72388": {
            "clubId": 72388,
            "name": "Last Kings ",
            "seasons": 0,
            "titlesWon": "0",
            "leaguesWon": "0",
            "divsWon1": 10,
            "divsWon2": 10,
            "divsWon3": 10,
            "divsWon4": 10,
            "cupsWon1": "0",
            "cupsWon2": "0",
            "cupsWon3": "0",
            "cupsWon4": "0",
            "dblsWon1": "0",
            "dblsWon2": "0",
            "dblsWon3": "0",
            "dblsWon4": "0",
            "cupsElim1": "0",
            "cupsElim2": "0",
            "cupsElim3": "0",
            "cupsElim4": "0",
            "promotions": "0",
            "holds": "0",
            "relegations": "0",
            "currentPoints": "1",
            "currentDivision": "1",
            "prevDivision": "1",
            "maxDivision": "1",
            "bestDivision": 0,
            "bestPoints": "0",
            "curSeasonMov": "-2",
            "lastMatch0": "0",
            "lastMatch1": "1",
            "lastMatch2": "-1",
            "lastMatch3": "-1",
            "lastMatch4": "-1",
            "lastMatch5": "-1",
            "lastOpponent0": "72179",
            "lastOpponent1": "72384",
            "lastOpponent2": "-1",
            "lastOpponent3": "-1",
            "lastOpponent4": "-1",
            "lastOpponent5": "-1",
            "starLevel": "1",
            "cupRankingPoints": "0",
            "overallRankingPoints": "1",
            "alltimeGoals": "2",
            "alltimeGoalsAgainst": "6",
            "seasonWins": "0",
            "seasonTies": "1",
            "seasonLosses": "1",
            "gamesPlayed": "2",
            "goals": "2",
            "goalsAgainst": "6",
            "points": "1",
            "projectedPoints": "-1",
            "prevSeasonWins": "0",
            "prevSeasonTies": "1",
            "prevSeasonLosses": "0",
            "prevPoints": "1",
            "prevProjectedPts": "-1",
            "skill": "1",
            "wins": "0",
            "ties": "1",
            "losses": "1"
        },
        "71361": {
            "clubId": 71361,
            "name": "Last Lap Mtsprt",
            "seasons": 0,
            "titlesWon": "0",
            "leaguesWon": "0",
            "divsWon1": 10,
            "divsWon2": 10,
            "divsWon3": 10,
            "divsWon4": 10,
            "cupsWon1": "0",
            "cupsWon2": "0",
            "cupsWon3": "0",
            "cupsWon4": "0",
            "dblsWon1": "0",
            "dblsWon2": "0",
            "dblsWon3": "0",
            "dblsWon4": "0",
            "cupsElim1": "1",
            "cupsElim2": "0",
            "cupsElim3": "0",
            "cupsElim4": "0",
            "promotions": "0",
            "holds": "0",
            "relegations": "0",
            "currentPoints": "1",
            "currentDivision": "1",
            "prevDivision": "1",
            "maxDivision": "1",
            "bestDivision": 0,
            "bestPoints": "0",
            "curSeasonMov": "-2",
            "lastMatch0": "0",
            "lastMatch1": "1",
            "lastMatch2": "0",
            "lastMatch3": "0",
            "lastMatch4": "0",
            "lastMatch5": "-1",
            "lastOpponent0": "72226",
            "lastOpponent1": "66842",
            "lastOpponent2": "71280",
            "lastOpponent3": "70071",
            "lastOpponent4": "71366",
            "lastOpponent5": "-1",
            "starLevel": "1",
            "cupRankingPoints": "5",
            "overallRankingPoints": "6",
            "alltimeGoals": "4",
            "alltimeGoalsAgainst": "12",
            "seasonWins": "0",
            "seasonTies": "1",
            "seasonLosses": "4",
            "gamesPlayed": "5",
            "goals": "4",
            "goalsAgainst": "12",
            "points": "1",
            "projectedPoints": "2",
            "prevSeasonWins": "0",
            "prevSeasonTies": "1",
            "prevSeasonLosses": "3",
            "prevPoints": "1",
            "prevProjectedPts": "2",
            "skill": "1",
            "wins": "0",
            "ties": "1",
            "losses": "4"
        },
        "68341": {
            "clubId": 68341,
            "name": "LAST LEGENDS",
            "seasons": 1,
            "titlesWon": "1",
            "leaguesWon": "0",
            "divsWon1": 10,
            "divsWon2": 10,
            "divsWon3": 10,
            "divsWon4": 10,
            "cupsWon1": "0",
            "cupsWon2": "0",
            "cupsWon3": "0",
            "cupsWon4": "0",
            "dblsWon1": "0",
            "dblsWon2": "0",
            "dblsWon3": "0",
            "dblsWon4": "0",
            "cupsElim1": "0",
            "cupsElim2": "0",
            "cupsElim3": "0",
            "cupsElim4": "0",
            "promotions": "1",
            "holds": "0",
            "relegations": "0",
            "currentPoints": "50",
            "currentDivision": "2",
            "prevDivision": "1",
            "maxDivision": "2",
            "bestDivision": 10,
            "bestPoints": "12",
            "curSeasonMov": "-2",
            "lastMatch0": "1",
            "lastMatch1": "2",
            "lastMatch2": "1",
            "lastMatch3": "1",
            "lastMatch4": "0",
            "lastMatch5": "0",
            "lastOpponent0": "68584",
            "lastOpponent1": "67494",
            "lastOpponent2": "68821",
            "lastOpponent3": "68670",
            "lastOpponent4": "63245",
            "lastOpponent5": "67555",
            "starLevel": "2",
            "cupRankingPoints": "0",
            "overallRankingPoints": "50",
            "alltimeGoals": "18",
            "alltimeGoalsAgainst": "19",
            "seasonWins": "0",
            "seasonTies": "1",
            "seasonLosses": "0",
            "gamesPlayed": "1",
            "goals": "3",
            "goalsAgainst": "3",
            "points": "1",
            "projectedPoints": "-1",
            "prevSeasonWins": "0",
            "prevSeasonTies": "0",
            "prevSeasonLosses": "0",
            "prevPoints": "0",
            "prevProjectedPts": "-1",
            "skill": "4",
            "wins": "3",
            "ties": "4",
            "losses": "3"
        },
        "41220": {
            "clubId": 41220,
            "name": "Last Xmas",
            "seasons": 10,
            "titlesWon": "8",
            "leaguesWon": "0",
            "divsWon1": 10,
            "divsWon2": 7,
            "divsWon3": 8,
            "divsWon4": 10,
            "cupsWon1": "0",
            "cupsWon2": "0",
            "cupsWon3": "0",
            "cupsWon4": "0",
            "dblsWon1": "0",
            "dblsWon2": "0",
            "dblsWon3": "0",
            "dblsWon4": "0",
            "cupsElim1": "0",
            "cupsElim2": "0",
            "cupsElim3": "0",
            "cupsElim4": "0",
            "promotions": "8",
            "holds": "2",
            "relegations": "0",
            "currentPoints": "1852",
            "currentDivision": "9",
            "prevDivision": "9",
            "maxDivision": "9",
            "bestDivision": 2,
            "bestPoints": "17",
            "curSeasonMov": "-2",
            "lastMatch0": "0",
            "lastMatch1": "0",
            "lastMatch2": "2",
            "lastMatch3": "0",
            "lastMatch4": "2",
            "lastMatch5": "2",
            "lastOpponent0": "2132",
            "lastOpponent1": "776",
            "lastOpponent2": "3896",
            "lastOpponent3": "941",
            "lastOpponent4": "2851",
            "lastOpponent5": "373",
            "starLevel": "8",
            "cupRankingPoints": "0",
            "overallRankingPoints": "1852",
            "alltimeGoals": "201",
            "alltimeGoalsAgainst": "105",
            "seasonWins": "6",
            "seasonTies": "0",
            "seasonLosses": "3",
            "gamesPlayed": "9",
            "goals": "15",
            "goalsAgainst": "14",
            "points": "18",
            "projectedPoints": "20",
            "prevSeasonWins": "6",
            "prevSeasonTies": "0",
            "prevSeasonLosses": "2",
            "prevPoints": "18",
            "prevProjectedPts": "22",
            "skill": "27",
            "wins": "60",
            "ties": "14",
            "losses": "12"
        },
        "42282": {
            "clubId": 42282,
            "name": "LastManStanding",
            "seasons": 0,
            "titlesWon": "0",
            "leaguesWon": "0",
            "divsWon1": 10,
            "divsWon2": 10,
            "divsWon3": 10,
            "divsWon4": 10,
            "cupsWon1": "0",
            "cupsWon2": "0",
            "cupsWon3": "0",
            "cupsWon4": "0",
            "dblsWon1": "0",
            "dblsWon2": "0",
            "dblsWon3": "0",
            "dblsWon4": "0",
            "cupsElim1": "0",
            "cupsElim2": "0",
            "cupsElim3": "0",
            "cupsElim4": "0",
            "promotions": "0",
            "holds": "0",
            "relegations": "0",
            "currentPoints": "3",
            "currentDivision": "1",
            "prevDivision": "1",
            "maxDivision": "1",
            "bestDivision": 0,
            "bestPoints": "0",
            "curSeasonMov": "-2",
            "lastMatch0": "2",
            "lastMatch1": "0",
            "lastMatch2": "-1",
            "lastMatch3": "-1",
            "lastMatch4": "-1",
            "lastMatch5": "-1",
            "lastOpponent0": "39619",
            "lastOpponent1": "37433",
            "lastOpponent2": "-1",
            "lastOpponent3": "-1",
            "lastOpponent4": "-1",
            "lastOpponent5": "-1",
            "starLevel": "1",
            "cupRankingPoints": "0",
            "overallRankingPoints": "3",
            "alltimeGoals": "3",
            "alltimeGoalsAgainst": "3",
            "seasonWins": "1",
            "seasonTies": "0",
            "seasonLosses": "1",
            "gamesPlayed": "2",
            "goals": "3",
            "goalsAgainst": "3",
            "points": "3",
            "projectedPoints": "-1",
            "prevSeasonWins": "0",
            "prevSeasonTies": "0",
            "prevSeasonLosses": "1",
            "prevPoints": "0",
            "prevProjectedPts": "-1",
            "skill": "1",
            "wins": "1",
            "ties": "0",
            "losses": "1"
        }
    }
}

正如您在此处的 json 的树状视图中看到的那样:http://prntscr.com/335ctx 服务器返回一个主要的“原始”对象,其中包含零到多个对象,每个对象都以大范围内的数字随机命名,并且每个对象都有一些其他常量对象。

我的基类是这样的:

public class ReSearchResponse
{
    public Raw2 raw { get; set; }
}

Raw2 类应该包含一些内容,告诉获取和保存“原始”对象的所有对象

通过这样做:

public class Raw2
{
    [JsonProperty(PropertyName="41220")]
    public Raw item {get; set;}
}

“item”成功获取服务器发送的所有数据,但仅适用于服务器返回名为“41220”的对象时。

欢迎任何帮助。

【问题讨论】:

    标签: c# json object deserialization


    【解决方案1】:

    假设Raw 是代表{"clubId": 41220, "name": "Last Xmas" ... } 的数据类型,那么您实际上要反序列化的是Dictionary<int, Raw>

    我没有对此进行测试,但您可以尝试使用以下方法:

    public class ReSearchResponse
    {
        public Dictionary<int, Raw> Data { get; set; }
    }
    

    【讨论】:

    • 它有效,当返回结果时。当返回的结果不包含任何内容并且主要的“原始”对象为空时,反序列化会挂起。但是,这很容易处理,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    相关资源
    最近更新 更多