Unity 中生成JSON以及对应的解析方法
来源:互联网 发布:android网络电话源码 编辑:程序博客网 时间:2024/05/17 02:27
注意:将库脚本或动态库(LitJSON.dll)置在Assets目录下的Plugins文件夹中,因为plugins文件夹中的脚本会先运行。
using LitJson; //注意引用命名空间using UnityEngine;public class LitJsonDemo : MonoBehaviour { // Use this for initialization void Start() { //============JSON的生成============================ JsonData data = new JsonData(); data["name"] = "Czhenya"; //生成嵌套结构的json data["info"] = new JsonData(); data["info"]["age"] = 28; data["info"]["sex"] = "男"; string js1= data.ToJson(); Debug.Log(js1); //============使用jsonMapper生成Json================== //此方法使用的前提是有一个玩家类 Player play1 = new Player(); play1.name = "czy"; play1.age = 22; play1.sex = "男"; string js2 = JsonMapper.ToJson(play1); Debug.Log(js2); //============使用JsonWritter生成Json================== JsonWriter js3 = new JsonWriter(); //对象类型的json js3.WriteObjectStart(); js3.WritePropertyName("name"); js3.Write("zhenya"); js3.WritePropertyName("age"); js3.Write("30"); js3.WritePropertyName("sex"); js3.Write("女"); js3.WriteObjectEnd(); Debug.Log(js3.ToString()); JsonWriter js4 = new JsonWriter(); //生成数组型json js4.WriteArrayStart(); js4.Write("CZHENYA"); js4.Write("25"); js4.Write("男"); js4.WriteArrayEnd(); Debug.Log(js4.ToString()); //需要注意的是有开始就要写结束 JsonWriter js5 = new JsonWriter(); //生成数组含对象 js5.WriteArrayStart(); //第一个对象 js5.WriteObjectStart(); js5.WritePropertyName("name"); js5.Write("zhenya"); js5.WritePropertyName("age"); js5.Write("30"); js5.WritePropertyName("sex"); js5.Write("女"); js5.WriteObjectEnd(); //第二个对象 js5.WriteObjectStart(); js5.WritePropertyName("name"); js5.Write("CZHENYA"); js5.WritePropertyName("AGE"); js5.Write("30"); js5.WritePropertyName("SEX"); js5.Write("男"); js5.WriteObjectEnd(); js5.WriteArrayEnd(); Debug.Log(js5.ToString()); //==========解析JSON 上面创建的Json1================ JsonData jsdata1 = JsonMapper.ToObject(js1); Debug.Log("解析json1 "+jsdata1["name"]+" "+jsdata1["info"]["age"]+""); //遍历JsonData的key由于JsonData实现了IDictionary接口,所以可以使用Foreach进行遍历 IDictionary dic = jsdata1 as IDictionary; foreach (string item in dic.Keys) { Debug.Log(item); } //===========解析上面的js2==================== Player play2 = JsonMapper.ToObject<Player>(js2); Debug.Log("解析js2 " + play2.name + " " + play2.age); //===========解析上面的js3,js4,js5 转换成字符串 js3为例==================== string json3 = js3.ToString(); JsonData jsdata3 = JsonMapper.ToObject(json3); Debug.Log("解析js3 "+jsdata3["name"]+jsdata3["age"]+jsdata3["sex"]); } //玩家类 public class Player { public string name { get; set; } public int age { get; set; } public string sex { get; set; } }}
上述代码结果:
阅读全文
1 0
- Unity 中生成JSON以及对应的解析方法
- java中json数据的生成以及解析
- 解析url的查询部分,生成对应的JSON对象
- Android中Gson解析和生成JSON字符串讲解,GsonFormat一键生成对应的数据对象
- Unity中Json创建,解析
- Unity中Json创建,解析
- Qt5中JSON的生成与解析
- json和对象转换以及相同不同对象的json解析为对应对象
- Android和Unity中,没有KEY的JSON数组解析
- unity中XML和JSON文档的解析
- Unity的Json解析--读取Json文件
- ajax中使用JSON的方法解析
- iOS json解析的4中方法
- Android Studio Json解析生成对应实体类 插件GsonFormat
- ndroid 网络请求json数据,解析json数据,生成对应的java bean类一步到位,快速开发
- Android 网络请求json数据,解析json数据,生成对应的java bean类一步到位,快速开发
- Android 网络请求json数据,解析json数据,生成对应的java bean类一步到位,快速开发
- Unity中使用Json解析字符串
- Springboot配置https访问
- dpdk学习之ip_pipeline源代码分析
- hadoop streaming参数整理
- SpringSecurity4最小化配置
- Linux(CentOS)下安装JDK
- Unity 中生成JSON以及对应的解析方法
- 面试题49:把字符串转换成整数
- 图像二值化 cv2.threshold
- Linux : 给硬盘分区
- 关于xgboost安装在anaconda上面(python3.6,64位,windows10)
- C++ 函数模板
- 0day安全——第四章小实验学习笔记
- 欢迎使用CSDN-markdown编辑器
- js实现点击切换checkbox背景图片