Parse Nested JSON with MiniJSON (Unity3D)

ⅰ亾dé卋堺 提交于 2019-12-11 01:05:47

问题


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.


回答1:


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"];


来源:https://stackoverflow.com/questions/22739791/parse-nested-json-with-minijson-unity3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!