C# JSON.net -Array like- Object Deserialization of non-constant name objects

时间秒杀一切 提交于 2019-12-25 02:06:15

问题


I'm trying to deserialize the json below:

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

As you can see in a tree-like view of the json here: http://prntscr.com/335ctx the server returns a main "raw" object which contains zero to many objects, each named randomly with numbers within a big range, and each of these objects have some other constant objects.

My base class is this:

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

The Raw2 class should contain something which tells gets and saves all the objects of the "raw" object

By doing this:

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

the "item" gets succesfully all the data sent from the server, but it's only for when the server returns the object named "41220" ONLY.

Any help is welcome.


回答1:


Assuming Raw is the datatype which represents {"clubId": 41220, "name": "Last Xmas" ... }, what you're actually trying to deserialize is a Dictionary<int, Raw>.

I haven't tested this, but you could try using the following:

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


来源:https://stackoverflow.com/questions/22582532/c-sharp-json-net-array-like-object-deserialization-of-non-constant-name-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!