user2018862
user2018862

Reputation: 93

Parse Nested JSON with MiniJSON (Unity3D)

i'm very newbie with JSON so i'm having problems with nested JSON's. I was searching two days without any luck, i saw a lot of examples of how to deserialize a nested JSON but my efforts failed so at last chance i'm here.

The thing i want to know is how i deserialize nested class with MiniJson, the JSONString is a facebook one, but i want to know the method for do it.

Here is an example of the JSONString i'm trying to deserialize.

{"data":
[{"user":{"name":"xxxxxxxxx1","id":"xxxxxxxxxx2"},"score":7,

"application":
{"name":"APPNAME","namespace":"APPNAMESPACE","id":"xxxxxxxxxx3"}}]}

Thanks in advance...

I tried with a lot of things this one is the last attempt i did, not a lot good but i was trying like a crazy to do it:

object dataObject;
            object scoreObject;
            var dict = Json.Deserialize(response.Text) as Dictionary<string,object>;
            Debug.Log(response.Text);


            var scores = new List<object>();
            if(dict.TryGetValue ("data", out scoreObject)) {
                Debug.Log("Hi");
                scores = (string)(((Dictionary<string, object>)scoreObject) ["score"]);
                if(scores.Count > 0) {
                    var scoreDict = ((Dictionary<string,object>)(scores[0]));
                    var score = new Dictionary<string, string>();
                    score["score"] = (string)scoreDict["score"];
                    Debug.Log((string)scoreDict["score"]);
                }
            }

PD: Sorry if my question is very noob or if i have a lot of negatives but it's really my last chance to understand something, thanks again.

Upvotes: 2

Views: 6460

Answers (1)

Radu Diță
Radu Diță

Reputation: 14191

Your problem lies in the fact that data contains an array and not an object, you need to get the first element of the data array and then get the value of score.

Something like this:

var dict = Json.Deserialize(response.Text) as Dictionary<string,object>;

List<object> scores = dict["data"] as List<object>;

Dictionary<string,object> scoreData = scores[0] as Dictionary<string,object>;

object score = scoreData["score"];

Upvotes: 2

Related Questions