私はゲーム用のプレーヤーの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" ]
}
]
}
は、私は現在、 プレーヤークラス
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 Oを使用しようとしているコードです。 bjectはnullではありませんが、Listは空です。誰かがJSONをPlayerListオブジェクトに変換してそこから取得する方法を教えてもらえれば、UserNameのようなプレイヤーの変数を得ることができます。
あなたのコードの問題は何ですか? –
私はこのサイトを最初にあなたの[Json2Csharp](http://json2csharp.com/)を生成することをお勧めします – MethodMan
PlayerList pListのプレーヤーにアクセスする方法を分かりません。 –