Hi guys, I’m completely stuck here. Hope I’m posting at the correct section.
How do I use SimpleJson to get the username and score of the first player in the following JSON:
[{"Player":{"username":"test","email":null,"first_name":"","last_name":"","platform":null},"Score":
{"score":"10","difficulty":0,"platform":null,"leaderboard":null,"created":"2014-04-22 08:26:57","data":null}}
,{"Player":{"username":"test","email":null,"first_name":"","last_name":"","platform":null},"Score":
{"score":"0","difficulty":0,"platform":null,"leaderboard":null,"created":"2014-04-22 08:29:44","data":null}}]
As a matter of fact, if you wish to suggest a different JSON reader, feel free to do so as long as you can show me the code that can pull this data.
Thanks in advance,
Arseney
http://wiki.unity3d.com/index.php/SimpleJSON
var N = JSON.Parse(the_JSON_string);
string username = N[0][“Player”][“username”];
int score = N[0][“Score”][“score”];
Code is untested.
Try formatting your string out better to see the entry points:
{
"Player":{
"username":"test",
"email":null,
"first_name":"",
"last_name":"",
"platform":null
},
"Score":{
"score":"10",
"difficulty":0,
"platform":null,
"leaderboard":null,
"created":"2014-04-22 08:26:57",
"data":null
}
},
{
"Player":{
"username":"test",
"email":null,
"first_name":"",
"last_name":"",
"platform":null
},
"Score":{
"score":"0",
"difficulty":0,
"platform":null,
"leaderboard":null,
"created":"2014-04-22 08:29:44",
"data":null
}
}
It looks to me that your json is an array of objects with Player and Score information.
So you’d get something like:
N = JSON.Parse(theJsonText);
var firstPlayer = N[0];
var username = firstPlayer["Player"]["username"].Value;
var score = firsePlayer["Score"]["score"].Value;
I haven’t used SimpleJSON, but that looks about right based on this article:
http://wiki.unity3d.com/index.php/SimpleJSON
lordofduct is correct. This should be the right workflow.
Thanks guys.
Apparently I’m just not used enough to JSONs. This was really easy and I’m a bit ashamed I failed at it.
Thanks again.