2015-09-09 47 views
2

我試圖反序列化來自Web服務器的JSON響應,我無法控制。正如你在下面看到的那樣,結果對象作爲JSONObject被返回。與此相關的問題是每個對象的關鍵對每個玩家都是唯一的。據我可以告訴我需要爲每個唯一的JSONObject創建一個單獨的類,以正確地反序列化字符串,根本無法工作,因爲每個對象的密鑰對於從Web服務器返回的每個創建的帳戶都是唯一的。我想知道是否有可能將JSONObject「播放器」反序列化爲JSONArray,或者如果我過多地忽略了這一點。玩家在列表中返回的ammount的變化也爲最多的32如何將JSONObject反序列化爲C#中的JSONArray#

{ 
"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; 
} 
+2

不知道你在使用什麼序列化器,但是如果你將'players'聲明爲'Dictionary ',那麼大多數應該做正確的事情。 – dbc

+3

我很確定我們不需要看到*所有*那個JSON - 我建議你把它裁減到兩個或三個球員的記錄... –

回答

1

嘗試是這樣的:

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

+0

你的小提琴不起作用。爲了解決這個問題,你需要(a)公開你的'Main'方法,並且(b)擺脫'Console.ReadKey()'。 –

+0

更新了它。謝謝。 – lintmouse

+0

沒問題。現在看起來不錯。 –