我有一個JSON遊戲玩家列表。我需要程序能夠讀取JSON文件,並給我球員列表。然後我可以使用這個球員列表來獲取每個球員的名字以及他們關注的人。將JSON文件讀入對象C#
{
"Version": "1",
"Users": [
{
"UserName": "CoolDude",
"Following": [ "SniperElitez", "IamAwesome" ]
},
{
"UserName": "GamerChick",
"Following": [ "IamAwesome", "NoWayBro", "WowWhoMe", "SniperElitez", "SurfingIsFun", "NowUTry" ]
},
{
"UserName": "SurfingIsFun",
"Following": [ "WowWhoMe" ]
},
{
"UserName": "IamAwesome",
"Following": [ "GamerChick", "NoWayBro" ]
},
{
"UserName": "NowUTry",
"Following": [ "GamerChick", "SniperElitez" ]
},
{
"UserName": "SniperElitez",
"Following": [ "SurfingIsFun", "IamAwesome" ]
},
{
"UserName": "WowWhoMe",
"Following": [ "NoWayBro", "GamerChick", "SniperElitez" ]
},
{
"UserName": "NoWayBro",
"Following": [ "GamerChick", "IamAwesome", "SniperElitez" ]
}
]
}
這裏是我想要使用目前 Player類
class Player
{
public string UserName { get; set;}
public List<string> Following { get; set;}
}
PlayerList類
class PlayerList
{
public List<Player> pList { get; set; }
}
主要
{
string json = File.ReadAllText("friends.json");
// this gives me a playerList object
PlayerList playerList = JsonConvert.DeserializeObject<PlayerList>(json);
}
按說PlayerList○碼bject不爲null,但列表爲空。如果任何人都可以告訴我如何將JSON轉換爲PlayerList對象,然後從那裏獲取玩家的變量,如UserName,我將非常感激!
那麼,你的代碼有什麼問題? –
我會建議嘗試這個網站最初生成您的[Json2Csharp](http://json2csharp.com/) – MethodMan
我不退出瞭解如何訪問PlayerList pList中的播放器。 –