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

查看:1056
本文介绍了使用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 数组,然后获取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天全站免登陆