- Home /
decode json response
Hi,
I get this json response from my server:
[
{
"playername": "Player 1",
"score": "88678",
"fbusername": "John Wheeler",
"gamelevel": "1"
},
{
"playername": "Player 6",
"score": "71515",
"fbusername": "John Wheeler",
"gamelevel": "1"
},
{
"playername": "Player 8",
"score": "1836",
"fbusername": "John Wheeler",
"gamelevel": "1"
}
]
and I cannot figure out how to get all the items out of the array.
Dictionary<string, object> search = Json.Deserialize(w.text) as Dictionary<string, object>;
foreach ( KeyValuePair <string, object> entry in search)
{
Debug.Log("key: " + entry.Key + ", value:" + entry.Value);
// do something with entry.Value or entry.Key
}
I would appreciate any help or explanation.
John
Update I managed to finally figure this out. The json returned is encased in [] which indicates an array of objects, here is the code I used together using $$anonymous$$iJson.
List<object> data = Json.Deserialize(w.text) as List<object>;
//Loop to traverse and process all the items returned from the server.
for (int i = 0; i < data.Count; i++)
{
fbid = System.Convert.ToString(((Dictionary<string, object>)data[i])["playername"]);
fbscore = System.Convert.ToString(((Dictionary<string, object>)data[i])["score"]);
fbusername = System.Convert.ToString(((Dictionary<string, object>)data[i])["fbusername"]);
fbgamelevel = System.Convert.ToString(((Dictionary<string, object>)data[i])["gamelevel"]);
Debug.Log("fbid = " + fbid);
Debug.Log("fbscore = " + fbscore);
Debug.Log("fbusername = " + fbusername);
Debug.Log("fbgamelevel = " + fbgamelevel);
}
Hopefully this will save someone else time wasting, like I had to do.
John
Answer by ShabihDesperado · Mar 23, 2015 at 05:00 PM
Try this webside to see if your JSON is correct: http://jsonlint.com/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get an object inside a json JsonUtility 0 Answers
JsonUtility doesn't serialize nested mixed var 1 Answer
Json.net - windows store phone 8.1 2 Answers