json - 使用 MiniJSON 解析嵌套 JSON (Unity3D)

标签 json facebook unity-game-engine nested

我对 JSON 非常新手,所以我在嵌套 JSON 方面遇到了问题。 我搜索了两天,没有任何运气,我看到了很多如何反序列化嵌套 JSON 的示例,但我的努力失败了,所以最后一次机会我在这里。

我想知道的是如何使用 MiniJson 反序列化嵌套类,JSONString 是 facebook 的,但我想知道执行此操作的方法。

这是我尝试反序列化的 JSONString 的示例

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

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

提前致谢...

我尝试了很多事情,这是我做的最后一次尝试,不是很好,但我疯狂地尝试去做:

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:很抱歉,如果我的问题很菜鸟或者我有很多负面的东西,但这确实是我理解某些事情的最后机会,再次感谢。

最佳答案

你的问题在于data包含一个数组而不是一个对象,你需要获取data数组的第一个元素,然后获取得分。

类似这样的事情:

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

关于json - 使用 MiniJSON 解析嵌套 JSON (Unity3D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22739791/

相关文章:

php - Volley 没有回应

javascript - 使用具有用户定义字段的新请求者创建新的 Zendesk 票证

json - Swift:给定的数据不是有效的 JSON

javascript - 页面选项卡重定向上的 IE 错误

facebook - Graph API 和位置隐私

ios - 编程中 "Fast script call and stripping"是什么意思

c# - 将 GetComponent<>() 与变量一起使用?

json - 使用 wso2 esb 中的聚合中介聚合多个 json 响应

android - 在 Unity 中使用 obb 时,3D 模型在运行时未更改

javascript - 如何创建一个滑动的 FB Like Box