今天在网上看到一个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. genymotion启动报错

    启动genymotion时提示 网络配置有问题,经检查是网卡virtualBox配置的问题,把ip设为自动获取即可 重新启动就可以了

  2. 005 android jni 一个简单的报错

    在android中使用ndk开发需要使用到jni. 1. java.lang.UnsatisfiedLinkError: No implementation found for void com.fr ...

  3. windows文件打包命令

    copy /Y partition.bin /b + qcsblhd_cfgdata.bin /b + qcsbl.bin /b + oemsbl.bin /b + fat.amss.bin /b + ...

  4. Linux文件类型及目录配置

    Linux文件类型与扩展名 在Linux系统中,任何硬件设备或者其他设备都是以文件的形式存在,就连数据通信的接口这些也是由专门的文件来负责的,因此Linux的文件种类就非常多,出了之前我们常见的 - ...

  5. Android Studio3.0 配置ButterKnife出错的解决

    需要注意的问题: (1)ButterKnife.bind(this);必须在设置布局之后进行初始化: 官方升级到了8.8.1了 compile 'com.jakewharton:butterknife ...

  6. 字符设备驱动程序--LED驱动

    编写驱动程序需要编写那些代码: 1.硬件相关的驱动程序 2.Makefile的编译程序 3.还需要编写一个相关的测试程序 比如说:一个摄像头驱动程序 1.驱动程序的编写,需要编写一些硬件相关的操作,编 ...

  7. activity通过流程实例id动态获取流程图并展示在jsp页面上

    提供的Service方法如下: Java /** * 获取当前任务流程图 * * @param processInstanceId * @return */ @Override public Inpu ...

  8. Digit Division

    Digit Division Time limit: 1 s Memory limit: 512 MiB We are given a sequence of n decimal digits. Th ...

  9. 关于Apache Shiro权限框架的一些使用误区的解释

    多了不说了,进入正题,shiro是个权限框架提供权限管理等功能,网上的教程一般都是互相抄,比如<shiro:principal property="xxx"/>这个标签 ...

  10. <2013 07 06> "极路由" 与 “家庭服务器” 报道两则

    跟我做!打造家庭服务器 很久没有更新了,因为之前托朋友帮我弄的mini PC终于到手了.阴差阳错地,原来只打算弄一台将就可用的低功耗下载机,结果到手的却是一台支持1080p(宣称,还没烧过),还带遥控 ...