今天在网上看到一个simplejson,直接调用这两个API就可以了,简单易用

string jsonstr = SimpleJson.SimpleJson.SerializeObject(json);
Debug.LogError(jsonstr);
TestJson test = SimpleJson.SimpleJson.DeserializeObject<TestJson>(jsonstr);

做了测试和JsonUtility.ToJson(json);几乎是一样的,但是JsonUtility.ToJson(json); 需要在每个类上加上[System.Serializable] 特性标签 ,并且只能用字段,不能用属性 ,都可以满足我们日常需求

using System.Collections;
using System.Collections.Generic;
using UnityEngine; [System.Serializable]
public class TestJson
{
[SerializeField] //经测试,加不加都可以
public string Name;
[SerializeField]
public Student[] stud;
}
[System.Serializable]
public class Student
{
[SerializeField]
public string School;
[SerializeField]
public Gender gender;
} [System.Serializable]
public class Gender
{
[SerializeField]
public int age;
[SerializeField]
public List<int> list;
} public class TestSimpliJson : MonoBehaviour
{ // Use this for initialization
void Start()
{
Tess();
}
void Tess()
{
TestJson json = new TestJson();
json.Name = "飞天小猪";
json.stud = new Student[]; for (int i = ; i < json.stud.Length; i++)
{
json.stud[i] = new Student();
json.stud[i].School = "飞天小猪" + i;
json.stud[i].gender = new Gender();
json.stud[i].gender.age = i;
json.stud[i].gender.list = new List<int>();
for (int j = ; j < i * i; j++)
{
json.stud[i].gender.list.Add(j);
}
} string jsonstr = SimpleJson.SimpleJson.SerializeObject(json);
Debug.LogError(jsonstr);
TestJson test = SimpleJson.SimpleJson.DeserializeObject<TestJson>(jsonstr);
string strJson = JsonUtility.ToJson(json);
Debug.LogError(strJson);
}
}

两种方式,输出如下

{"Name":"飞天小猪","stud":[{"School":"飞天小猪0","gender":{"age":0,"list":[]}},{"School":"飞天小猪1","gender":{"age":1,"list":[0]}},{"School":"飞天小猪2","gender":{"age":2,"list":[0,1,2,3]}},{"School":"飞天小猪3","gender":{"age":3,"list":[0,1,2,3,4,5,6,7,8]}}]}

不过如果不给list赋值,稍微有点不一样的地方

{"Name":"飞天小猪","stud":[{"School":"飞天小猪0","gender":{"age":0,"list":null}},{"School":"飞天小猪1","gender":{"age":1,"list":null}},{"School":"飞天小猪2","gender":{"age":2,"list":null}},{"School":"飞天小猪3","gender":{"age":3,"list":null}}]}

{"Name":"飞天小猪","stud":[{"School":"飞天小猪0","gender":{"age":0,"list":[]}},{"School":"飞天小猪1","gender":{"age":1,"list":[]}},{"School":"飞天小猪2","gender":{"age":2,"list":[]}},{"School":"飞天小猪3","gender":{"age":3,"list":[]}}]}

Unity Json 之三的更多相关文章

  1. 欢快的使用Unity JSON吧

    0x01:前言 Unity 5.3加入了UnityUtility类,意味着Unity终于有了自己原生态的JSON库.Unity主要用来游戏开发,JSON做为游戏开发中最受欢迎的配置文件.在官方没有库支 ...

  2. Unity Json解析IPA

    今天看到一个unity 自带的解析json的IPA,感觉比litjson好用很多,废话不多,上代码 using System.Collections; using System.Collections ...

  3. JSON之三:获取JSON文本并解释(以google的天气API为例)

    google提供了天气的api,以广州天气为例,地址为: http://api.openweathermap.org/data/2.5/weather?q=guangzhou 返回的结果为: {   ...

  4. Unity json

    MiniJSON.cs using UnityEngine; using System; using System.Collections; using System.Collections.Gene ...

  5. unity json解析IPA后续

    以前说到的,有很大的限制,只能解析简单的类,如果复杂的就会有问题,从老外哪里看到一片博客,是将类中的list   等复杂对象序列化, using UnityEngine; using System.C ...

  6. Unity的Json解析<二>–写Json文件

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50378805 作者:car ...

  7. C#开源

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

  8. C# 开源项目一

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

  9. C#开源大全--汇总(转)

    商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...

随机推荐

  1. Linux 常用文件管理命令

    Command Description cat filename 查看文件内容.举例:cat pushticketfast.sh   --最好用tail -n 500  cd dirname 改变所在 ...

  2. centos7 笔记本盒盖不睡眠

    cd /etc/systemd vi logind.conf 动作包括:HandlePowerKey:按下电源键后的动作HandleSleepKey:按下挂起键后的动作HandleHibernateK ...

  3. ChemDraw破解版真的不大好用

    一直以来都有很多的用户朋友在网上找ChemDraw破解版使用,但是现在厂商清理的厉害,还有国家对知识产权的保护越来越严格,破解版ChemDraw越来越难找了.大家与其花那么多的时候找破解版的,不如买个 ...

  4. 制作dos启动u盘

    需要准备的工具: 空U盘的U盘HP优盘启动盘格式化工具 链接:https://pan.baidu.com/s/1i59wgUp 密码:l5ke 1.1插入U盘,打开   HP优盘启动盘格式化工具 1. ...

  5. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.5——在flavors间合并java代码

    问题: 你想要在单独的product flavors里面增加Acitivity或者其它java类. 解决方案: 创建合适的代码目录,增加java类,将它们和main代码合并. 讨论: flavors和 ...

  6. BZOJ 2431

    2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1521  Solved: 883[Submit][Statu ...

  7. The Intriguing Obsession

    C. The Intriguing Obsession time limit per test 1 second memory limit per test 256 megabytes input s ...

  8. Android实现splash

    笔者近日遇到一个android中双splash的问题.要求先实现百度的logo,在接入自己的logo. public class MainActivity extends BaseActivity { ...

  9. Quality of service

    w https://en.wikipedia.org/wiki/Quality_of_service Quality of service (QoS) is the overall performan ...

  10. [转】[tip] localhost vs. (local) in SQL Server connection strings

    主要区别在于连接协议不同,前者(localhost)使用TCP协议,后者("(local)")使用NamedPipe协议. Sample code with SQL Server ...