JsonUtility如何解析下面的ajax解析json数组组

JsonUtility array not supported? - Unity Answers
Navigation
Unity account
You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
JsonUtility array not supported?
I'm reading the documentation on
and find it hard to believe, that I can't serialize or deserialize an array of serializable objects. This works fine with plugins like LitJson, I was using before, so now I thought it was great, that Unity has json support built-in. Am I doing something wrong or is this maybe a feature to come in the future?
// Create some already tested dummy data.
User user = new User();
user.DisplayName = &TestName&;
// Works fine with single object
string json = JsonUtility.ToJson(user);
Debug.Log(json); // returns valid json string.
Debug.Log(JsonUtility.FromJson&User&(json)); // works as it should.
// Doesn't work in array.
User[] usersArray = new User[3] {user,user,user};
string json2 = JsonUtility.ToJson(usersArray);
Debug.Log(json2); // returns &{}&
Debug.Log(JsonUtility.FromJson&User&(json2)); // is empty.
Thanks! :)
Best Answer
Serializing/deserializing arrays and lists as top-level elements is not supported right now. It's on the to-do list...
Note that you can work around it in two ways for now:
Use ToJson() for each element in the array separately, and just stitch them together with &[&, &,& and &]&
Wrap the array in a structure like this:
[Serializable] public struct MyObjectArrayWrapper { public MyObject[] }
Here is my implementation of a JsonUtility wrapper for arrays and lists and also nested arrays and lists.
void ParseJsonToObject(string json)
var wrappedjsonArray = JsonUtility.FromJson&MyWrapper&(json);
[Serializable]
private class MyWrapper
public List&MyObject&
[Serializable]
private class MyObject
public int variable1;
public string variable2;
public List&MyNestedObject& nestedO
[Serializable]
private class MyNestedObject
public string nestedVariable1;
public string nestedVariable2;
And the json would look like this
{ &objects&: [
&variable1&: &abc123&,
&variable2&: &def456&,
&variable3&: [
&nestedVariable1&: &ghi789&,
&nestedVariable2&: &jkl101&
&nestedVariable1&: &zxy789&,
&nestedVariable2&: &wfg101&
&variable1&: &3qaghh5&,
&variable2&: &34qhah&,
&variable3&: [
&nestedVariable1&: &3qwe54hb&,
&nestedVariable2&: &4ahb4e&
&nestedVariable1&: &gverazh4&,
&nestedVariable2&: &233ghs&
a simple wrapper to support array
public JsonHelper ()
//YouObject[] objects = JsonHelper.getJsonArray&YouObject& (jsonString);
public static T[] getJsonArray&T&(string json)
string newJson = &{ \&array\&: & + json + &}&;
Wrapper&T& wrapper = JsonUtility.FromJson&Wrapper&T&& (newJson);
return wrapper.
//string jsonString = JsonHelper.arrayToJson&YouObject&(objects);
public static string arrayToJson&T&(T[] array)
Wrapper&T& wrapper = new Wrapper&T& ();
wrapper.array =
return JsonUtility.ToJson (wrapper);
[Serializable]
private class Wrapper&T&
public T[]
I just tested it in Unity 5.3.5 declaring the field like this:
public Obbj[] obbs = new Obbj[] { new Obbj(), new Obbj() };
And it works :) It seems they added the array support
Hint: You can notify a user about this post by typing @username
Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
70 People are following this question.Unity3d的5.4版使用JsonUtility.FromJson序列化JSON,多次使用后数据不更新_百度知道
Unity3d的5.4版使用JsonUtility.FromJson序列化JSON,多次使用后数据不更新
首先,我第一次获取的json是这样的:json={&books&:[],&pagecount&:8,&curpage&:0,&err&:0,&msg&:&success&}通过JsonUtility.FromJson&Books&(json);成功解析。然后,我再次访问服务器...
首先,我第一次获取的json是这样的:json = {&books&:[],&pagecount&:8,&curpage&:0,&err&:0,&msg&:&success&}通过JsonUtility.FromJson&Books&(json); 成功解析。然后,我再次访问服务器,获取到的json是这样的json = {&books&:[],&pagecount&:8,&curpage&:1,&err&:0,&msg&:&success&}再次通过JsonUtility.FromJson&Books&(json);来进行解析,这次就不对了,读取出来的curpage 始终还是保持前一次读取的结果 为 0.后来,我经历N次访问服务器,获取到json:json = {&books&:[],&pagecount&:8,&curpage&:N,&err&:0,&msg&:&success&}但是使用FromJson解析出来 curpage 始终是 0 。我跟踪调试的时候看到在进行序列化之前,json文本中的值确实是新获取到的值,而用来存放结果的对象object也是new出来的,里面也不会有旧值,但是为什么FromJson之后,把旧值丢进来了呢?而在json中,books数组里面的值却可以正确得到。补充:我访问服务器提取json是使用 WWW。WWW w = new WWW(url);json = w.求教高手,在线等。谢谢。
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
采纳数:91
获赞数:472
检查下返回的json的字段的key,和你的类中的属性名是否一致,再检查下该属性的getter setter方法
为你推荐:
其他类似问题
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。Unity做json 是用 自带这个JsonUtility 还是外部的?
关键我是你雨哥哥你们做json&是用&自带这个JsonUtility&还是外部的?Kei这个我还没关注,稍后看看用的地方好像是个叫jsonobject的第三方库ant123mark&【广州】主美,地编,UI组长,2D特效,角色原画组长(日韩写实、二次元、日式机甲、国古风)、场景原画、主策,数值策划,高级文案、资深U3D或主程、服务器程序、法务总监【北京】3D/2D动作、3D特效、角色原画、场景原画、资深UI、U3D主程、AS3主程、ACT关卡策划、ACT战斗策划【杭州】U3D程序、各类美术、高级文案【上海】原画组长、&U3D引擎、服务器C++、UE策划、数值策划【深圳】服务器C++(多人对战&MMO)【成都】海外运营经理、次世代角色、地编、国风写实角色原画、场景原画【珠海】各类美术、U3D程序、服务端C++、UE4测试开发、棋牌测试经理、棋牌运营经理、棋牌技术经理欢迎小窗,新手实习生勿扰,需要2年以上工作经验简历接收邮箱LaPlimoy干货铺子丨如何实现类Unity3D层级游戏对象http://gad.qq.com/article/detail/7174131?ADTAG=gad.cx.qq分享一篇干货可以看看与世无争狗Alpha狗手动再见关键我是你雨哥哥那是啥啊。表示没听过。。。飘装不起苹果&不知道关键我是你雨哥哥二叶,是什么jb玩意。灰狗@与世无争狗&劳资用VSAlpha狗那是eclipse灰狗.飘我去.可以不可以pulic&fuck灰狗只能&Private&Fuck飘@灰狗&这个说的是你&啊灰狗飘这周只能看别人玩乱斗了穷B玩不起关键我是你雨哥哥我日,eclipse&长那样哦。。装什么,玩得起股票的人在这喊穷?灰狗确实,这个Eclipse的Icon好搓!飘今天涨了个饭钱关键我是你雨哥哥好有钱,飘我吃个饭才10块不像你&&一天顶我一周了关键我是你雨哥哥我已经好久不知道外面吃饭是什么了。我都是自己做好带到公司热一热就对付了。美途王元洪谁找到,微信SDK&分享动态图&咋整,我咋分享出来不动呢杭州-坏老头手里有几百块,能买到股票吗美途王元洪能,虚拟股市,可以,飘开户就行了&不要钱Bigger-酱油取几毛钱的股票又不是没有关键我是你雨哥哥看见两个妹子穿着丝袜打乒乓球,很有喜感。不知道是哪个公司的,好想加入她们。大金毛无图无真相仙人盏无图无真相关键我是你雨哥哥在那边大厅,你撒尿还带着手机吗?ant不如跳舞系列仙人盏可以再撒一次ant
------分隔线----------------------------
赞助商链接
赞助商链接扫一扫体验手机阅读
JsonUtility一个小的注意事项
<span type="1" blog_id="2072571" userid='
45篇文章,8W+人气,0粉丝Json数据解析在Unity3d中的应用
最近做项目过程中因为Json文件名写错了一个字母Unity报错,找错误找到半夜,当时为了验错,写了一个小Demo,正好借此总结一下Json.
1.什么是Json
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。
2.JSON 语法规则
数据在键值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 3.Json的结构
对象:对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。
数组:数组在js中是中括号“[]”括起来的内容,数据结构为 ["java","javascript","vb",...],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。
4.Json在Unity3d中的使用
JsonUtility:JsonUtility是Untiy5.3后新增的内容,有时会出现莫名其妙的错误:
JsonUtility.FromJson():把json字符串转成对象.
JsonUtility.ToJson():把对象转成json字符串.
LitJson:LitJson是一个Json的开源项目,这个比较稳定,推荐使用:
JsonMapper.ToObject():把json字符串转成对象.
JsonMapper.ToJson():把对象转成json字符串.
官网下载:http://www.json.org/json-zh.html
放到Unity3d中的Plugin文件夹内
使用的话要注意引入命名空间
纯数组的Json数据比较简单,在这个Demo中Json数据是一个对象,对象内有一个数组.分别需要用到一个Json对象模型(JsonObjectModel)和一个Json数组模型(JsonArrayModel)来接收这两种Json数据:
Json源文件 {
"infoList": [
"panelTypeString": "ItemMessage",
"path": "UIPanel/ItemMessagePanel"
"panelTypeString": "Knapsack",
"path": "UIPanel/KnapsackPanel"
"panelTypeString": "MainMenu",
"path": "UIPanel/MainMenuPanel"
"panelTypeString": "Shop",
"path": "UIPanel/ShopPanel"
"panelTypeString": "Skill",
"path": "UIPanel/SkillPanel"
"panelTypeString": "System",
"path": "UIPanel/SystemPanel"
"panelTypeString": "Task",
"path": "UIPanel/TaskPanel"
JsonObjectModel:Json对象模型 public class JsonObjectModel
//JsonArrayModel类型的列表
public List&JsonArrayModel& infoL
JsonArrayModel:Json数组模型 public class JsonArrayModel
//对应Json中属性 名字要一样
public string panelTypeS
JsonDeserialize:Json解析程序,挂在场景中JsonManager上 public class JsonDeserialize : MonoBehaviour
void Start( )
//获取Json文件
TextAsset jsonData = Resources.Load&TextAsset&("JsonData");
JsonObjectModel jsonObject = JsonMapper.ToObject&JsonObjectModel&(jsonData.text);
foreach (var info in
jsonObject.infoList)
Debug.Log(info.panelTypeString+" "+info.path);
输出结果:6.Demo源代码
链接:http://pan.baidu.com/s/1jHVP9m2 密码:e9h8
Unity3d Json解析库
Unity3d之json解析研究
Unity3D——LitJson解析json,读取关卡信息
没有更多推荐了,}

我要回帖

更多关于 ajax解析json数组 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信