使用LitJson插件
LitJson使用
只需要把ListJson.dll拖进Unity Assets文件夹中即可在代码中引用。
我们先简单封装一下Litjson以便更方便于Unity使用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//引入插件
using System.IO;
using System.Text;
public class JsonDataTool
{
public static List<T> GetListFromJson<T>(string path)
{//获取json数据
string str = File.ReadAllText(Application.dataPath + path, Encoding.GetEncoding("UTF-8"));//读取Json字符串
if (str == null) Debug.LogError("未找到目标资源:" + path);
List<T> list = JsonMapper.ToObject<List<T>>(str);//使用Litjson的方法将字符串转化为链表
return list;
}
public static void SetJsonFromList<T>(string path, List<T> list)//此方法常用于存档的实现
{//修改json数据
string jsonstr = JsonMapper.ToJson(list);
File.WriteAllText(Application.dataPath + path,