使用 MiniJSON (Unity3D) 解析嵌套的 JSON [英] Parse Nested JSON with MiniJSON (Unity3D)

查看:93
本文介绍了使用 MiniJSON (Unity3D) 解析嵌套的 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 JSON 非常新手,所以我在使用 嵌套 JSON 时遇到了问题.我搜索了两天没有任何运气,我看到了很多关于如何反序列化嵌套 JSON 的示例,但我的努力失败了,所以我终于有机会来了.

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.

我想知道的是我如何使用 MiniJson 反序列化嵌套类,JSONString 是 Facebook 的,但我想知道这样做的方法.

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.

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

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

提前致谢...

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

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:对不起,如果我的问题很菜,或者我有很多负面的东西,但这真的是我最后一次了解某些东西的机会,再次感谢.

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.

推荐答案

你的问题在于data包含的是数组而不是对象,你需要获取data的第一个元素strong>data 数组,然后获取 score 的值.

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.

像这样:

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

这篇关于使用 MiniJSON (Unity3D) 解析嵌套的 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆