Unity 基础 之 Litjson 简单进行json 创建与解析
目录
Unity 基础 之 Litjson 简单进行json 创建与解析
七、后记(在Unity中读取Resource 中的json数据,一次解析json 数据列表)
一、简单介绍
Unity中的一些基础知识点。
Unity游戏开发中,进行数据封装是很常见的操作,这里简单介绍使用 litjson ,进行json数据的创建和解析。
二、实现原理
1、JsonMapper.ToJson将制定数据结构转换为Json字符,进行 Json 数据创建
2、JsonMapper.ToObject<T>用于将Json字符转换为指定数据结构,进行 Json 数据解析
三、注意事项
1、请记得导入 Litjson.dll 插件
2、注意解析的使用 结构要保有 无参构造,不然 JsonMapper.ToObject<T> 可能会报错哈
四、效果预览
五、实现步骤
1、打开Unity,新建一个空工程,导入 Litjson.dll,如下图
2、新建脚本,使用Litjson ,进行数据的Json 创建与解析,如下图
3、把脚本挂载到场景中,如下图
4、运行场景,控制台 Console 打印如下
六、关键代码
1、JsonTest
using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero
{
public string name;
public int level;
public int attack;
// 这个要有,不然JsonMapper.ToObject<T>(jsonStr) 会报错
public Hero(){}
public Hero(string name, int level, int attack)
{
this.name = name;
this.level = level;
this.attack = attack;
}
}
public class Enemy
{
public string name;
public int attack;
public string[] devices;
// 这个要有,不然JsonMapper.ToObject<T>(jsonStr) 会报错
public Enemy() { }
public Enemy(string name, int attack, string[] devices)
{
this.name = name;
this.attack = attack;
this.devices = devices;
}
}
public class Data
{
public string version;
public Hero[] heros;
public Enemy[] enemys;
// 这个要有,不然JsonMapper.ToObject<T>(jsonStr) 会报错
public Data(){}
public Data(string version, Hero[] heros, Enemy[] enemys)
{
this.version = version;
this.heros = heros;
this.enemys = enemys;
}
}
public class JsonTest : MonoBehaviour
{
Data data;
// Start is called before the first frame update
void Start()
{
Init();
string jsonStr = DataObjectToJson(data);
Debug.Log("DataObjectToJson :" + jsonStr);
Data jsonToObject = DataJsonToObject<Data>(jsonStr);
Debug.Log("DataJsonToObject:");
Debug.Log("version:" + jsonToObject.version);
Debug.Log("heros:");
foreach(Hero hero in jsonToObject.heros){
Debug.Log(hero.name + " " + hero.level + " " +hero.attack);
}
Debug.Log("enemys:");
foreach (Enemy enemy in jsonToObject.enemys)
{
Debug.Log(enemy.name + " " + enemy.attack);
foreach (string str in enemy.devices) {
Debug.Log(str);
}
}
}
// Update is called once per frame
void Update()
{
}
void Init() {
Hero superman = new Hero("SuperMan",20,345);
Hero batman = new Hero("BatMan",19,321);
Hero spiderman = new Hero("SpiderMan",19,301);
Enemy joker = new Enemy("Joker",298,new string[] { "gun","knife"});
Enemy blackWidow = new Enemy("BlackWidow", 301, new string[] { "gun", "knife" });
data = new Data("1.0.2", new Hero[] {superman,batman,spiderman}, new Enemy[] {joker,blackWidow });
}
/// <summary>
/// 转为json数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
string DataObjectToJson<T>(T t) {
return JsonMapper.ToJson(t);
}
/// <summary>
/// json 转为 object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonStr"></param>
/// <returns></returns>
T DataJsonToObject<T>(string jsonStr) {
return JsonMapper.ToObject<T>(jsonStr);
}
}
七、后记(在Unity中读取Resource 中的json数据,一次解析json 数据列表)
JsonMapper.ToObject<List<Field>>(jsonString)
1、代码函数
public class JsonParse:MonoBehaviour
{
List<Field> LocalArrowBleList = new List<Field>();
IEnumerator GetLocalBowBeacons()
{
yield return null;
TextAsset t = Resources.Load("BowBeacon") as TextAsset;
string s = t.text;
LocalArrowBleList = JsonMapper.ToObject<List<Field>>(s);
StreamWriter sw = new StreamWriter(Application.dataPath + "arrowbeacons_0310_02.json", false, Encoding.UTF8);
if((s != null))
{
Debug.Log(s);
sw.WriteLine(s);
}
sw.Close();
Debug.Log(LocalArrowBleList.Count.ToString());
}
}
/// <summary>
/// json数据结构
/// </summary>
public class Field
{
public string mac;
public string device_id;
public bool device_is_using;
public string comment;
public Field() { }
public Field(string mac, string id, bool state, string comment) {
this.mac = mac;
this.device_id = id;
this.device_is_using = state;
this.comment = comment;
}
}
2、json 数据 (文件名:BowBeacon.txt)
[{"mac":"FE:38:78:B7:3C:D4","device_id":"PFBOW00001","device_is_using":true,"comment":"北京市使用"},
{"mac":"FE:38:78:B7:3C:FA","device_id":"PFBOW00002","device_is_using":true,"comment":"北京市使用"},
{"mac":"FE:38:78:B7:3C:D8","device_id":"PFBOW00003","device_is_using":true,"comment":"北京市使用"},
{"mac":"FE:38:78:B7:3C:E5","device_id":"PFBOW00004","device_is_using":true,"comment":"北京市使用"},
{"mac":"00:00:00:70:00:23","device_id":"PFBOW00088","device_is_using":true,"comment":"深圳市使用"},
{"mac":"00:00:00:70:00:2E","device_id":"PFBOW00081","device_is_using":true,"comment":"深圳市使用"},
{"mac":"00:00:00:70:00:30","device_id":"PFBOW00069","device_is_using":true,"comment":"深圳市使用"},
{"mac":"FE:38:78:B7:3D:91","device_id":"PFBOW00010","device_is_using":true,"comment":"深圳市使用"},
{"mac":"FE:38:78:B7:3C:D0","device_id":"PFBOW001HY","device_is_using":true,"comment":"武汉市火油使用"},
{"mac":"FE:38:78:00:01:02","device_id":"PFBOW002HY","device_is_using":true,"comment":"武汉市火油使用"}]