我想使用 MiniJson 解析 Json 并将其存储在列表中以供操作
I want Parse Json using MiniJson and store it in a list for manipulation
{
"players": [
{
"wins": 8,
"losses": 1,
"score": 264.5,
"pro": false,
"name": "albert"
},
{
"wins": 7,
"losses": 1,
"score": 214.5,
"pro": false,
"name": "mike"
}
我想解析它,我试过如下:
Dictionary<string,object> data=MiniJSON.Json.Deserialize(json) as Dictionary<string,object>;
List<object> list=(List<object>)(data["Person"]);
//data["players"] as List<object>
foreach(var item in data)
{
// data as Dictionary<string,object>
Dictionary<string,object> dict=(Dictionary<string,object>)(data);
}
我试过了但是我不知道它是否正确而且我无法继续进行下去请
抽出一分钟帮助我
1) 您的 Json 无效:
您可以使用此 site 来验证 json。您应该添加“]”和“}”。所以你 json 应该看起来像:
{
"players":
[
{
"wins": 8,
"losses": 1,
"score": 264.5,
"pro": false,
"name": "albert"
},
{
"wins": 7,
"losses": 1,
"score": 214.5,
"pro": false,
"name": "mike"
}
]
}
2) 您可以使用 Unity JsonUtility。
创建class个玩家
[Serializable]
public class Player
{
int wins;
int losses;
int score;
bool pro;
string name;
}
然后创建播放器容器
public class PlayerContainer
{
List<Player> players = new List<Player>();
}
现在您可以使用 JsonUtility serialize/deserialize 您的数据
public class PlayersController
{
private void Start()
{
string json = "your json";
var players = JsonUtility.FromJson<PlayerContainer>(json);
}
}
{
"players": [
{
"wins": 8,
"losses": 1,
"score": 264.5,
"pro": false,
"name": "albert"
},
{
"wins": 7,
"losses": 1,
"score": 214.5,
"pro": false,
"name": "mike"
}
我想解析它,我试过如下:
Dictionary<string,object> data=MiniJSON.Json.Deserialize(json) as Dictionary<string,object>;
List<object> list=(List<object>)(data["Person"]);
//data["players"] as List<object>
foreach(var item in data)
{
// data as Dictionary<string,object>
Dictionary<string,object> dict=(Dictionary<string,object>)(data);
}
我试过了但是我不知道它是否正确而且我无法继续进行下去请 抽出一分钟帮助我
1) 您的 Json 无效: 您可以使用此 site 来验证 json。您应该添加“]”和“}”。所以你 json 应该看起来像:
{
"players":
[
{
"wins": 8,
"losses": 1,
"score": 264.5,
"pro": false,
"name": "albert"
},
{
"wins": 7,
"losses": 1,
"score": 214.5,
"pro": false,
"name": "mike"
}
]
}
2) 您可以使用 Unity JsonUtility。 创建class个玩家
[Serializable]
public class Player
{
int wins;
int losses;
int score;
bool pro;
string name;
}
然后创建播放器容器
public class PlayerContainer
{
List<Player> players = new List<Player>();
}
现在您可以使用 JsonUtility serialize/deserialize 您的数据
public class PlayersController
{
private void Start()
{
string json = "your json";
var players = JsonUtility.FromJson<PlayerContainer>(json);
}
}