unity — Json解析
程序员文章站
2022-03-28 11:06:19
...
创建一个AppOfTriggerPhone类:
public class AppOfTriggerPhone
{
public int appNum;
public bool PhoneState;
public List<string> appList;
}
在创建Assets下创建Resources文件夹下创建Json文件夹
如:
创建JsonTest脚本,声明写入和读取方法:
在Start中写入Json
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class JsonText : MonoBehaviour
{
private AppOfTriggerPhone appOfTriggerPhone;
private void Start()
{
// 简单的Json解析
appOfTriggerPhone = new AppOfTriggerPhone()
{
appNum = 3,
PhoneState = true,
appList = new List<string>()
{
"抖音","Bilibili","绝地求生"
}
};
SaveByJson();
}
public void SaveByJson()
{
string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";
string saveJsonStr = JsonMapper.ToJson(appOfTriggerPhone);
StreamWriter sw = new StreamWriter(filePath);
sw.Write(saveJsonStr);
sw.Close();
}
public AppOfTriggerPhone LoadByJson()
{
AppOfTriggerPhone appGO = new AppOfTriggerPhone();
string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";
if (File.Exists(filePath))
{
StreamReader sr = new StreamReader(filePath);
string jsonStr = sr.ReadToEnd();
sr.Close();
appGO = JsonMapper.ToObject<AppOfTriggerPhone>(jsonStr);
}
if (appGO == null)
{
Debug.Log("读取Json信息失败");
}
return appGO;
}
}
再在Start方法中读取:
appOfTriggerPhone = new AppOfTriggerPhone();
appOfTriggerPhone = LoadByJson();
Debug.Log(appOfTriggerPhone.appNum);
Debug.Log(appOfTriggerPhone.PhoneState);
foreach (var item in appOfTriggerPhone.appList)
{
Debug.Log(item);
}
复杂的Json读取:
创建新的类AppProperty,并修改AppOfTriggerPhone
public class AppProperty {
public string appName;
public string triggerID;
public bool triggerFavour;
public List<int> useTimeList;
}
public class AppOfTriggerPhone
{
public int appNum;
public bool PhoneState;
public List<AppProperty> appList;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class JsonText : MonoBehaviour
{
private AppOfTriggerPhone appOfTriggerPhone;
private void Start()
{
//复杂的Json解析
appOfTriggerPhone = new AppOfTriggerPhone
{
appNum = 3,
PhoneState = true,
appList = new List<AppProperty>()
};
AppProperty appProperty = new AppProperty
{
appName = "抖音",
triggerID = "Trigger",
triggerFavour = true,
useTimeList = new List<int> { 6, 7, 8 }
};
appOfTriggerPhone.appList.Add(appProperty);
SaveByJson();
appOfTriggerPhone = LoadByJson();
Debug.Log(appOfTriggerPhone.appNum);
Debug.Log(appOfTriggerPhone.PhoneState);
foreach (var item in appOfTriggerPhone.appList)
{
Debug.Log(item);
Debug.Log(item.appName);
Debug.Log(item.triggerFavour);
Debug.Log(item.triggerID);
foreach (var itemGo in item.useTimeList)
{
Debug.Log(itemGo);
}
}
}
public void SaveByJson()
{
string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";
string saveJsonStr = JsonMapper.ToJson(appOfTriggerPhone);
StreamWriter sw = new StreamWriter(filePath);
sw.Write(saveJsonStr);
sw.Close();
}
public AppOfTriggerPhone LoadByJson()
{
AppOfTriggerPhone appGO = new AppOfTriggerPhone();
string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";
if (File.Exists(filePath))
{
StreamReader sr = new StreamReader(filePath);
string jsonStr = sr.ReadToEnd();
sr.Close();
appGO = JsonMapper.ToObject<AppOfTriggerPhone>(jsonStr);
}
if (appGO == null)
{
Debug.Log("读取Json信息失败");
}
return appGO;
}
}
运行结果:
推荐阅读
-
JavaScript实现的反序列化json字符串操作示例
-
Unity3D中自动调用的方法总结
-
iOS开发中常见的项目文件与MVC结构优化思路解析
-
新版上线!解析PSD文件管理控件Aspose.PSD实用功能——色彩平衡调整层
-
在mybatis和PostgreSQL Json字段作为查询条件的解决方案
-
Mybaits 源码解析 (八)----- 全网最详细,没有之一:结果集 ResultSet 自动映射成实体类对象(上篇)
-
Mybaits 源码解析 (九)----- 全网最详细,没有之一:一级缓存和二级缓存源码分析
-
死磕 java线程系列之ForkJoinPool深入解析
-
JS基础语法---预解析
-
asp.net abp模块化开发之通用树2:设计思路及源码解析