一、读取
Unity读取Json算是一个很常用的功能,今天来小小的总结一下。
首先你得写个json,百度Json可以找到很多接送工具。
比如我写的json,注意:因为有中文,所以要保存UTF-8的格式。后缀名可以为.txt 也可以为.json
第一种,JsonData读取的方式,这种方法比较直接,先把数据拿过来再处理
StreamReader streamreader = new StreamReader(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数据流
JsonReader jsonReader = new JsonReader(streamreader); //转换成json数据
JsonData jsonData = JsonMapper.ToObject(jsonReader); //转化为jsonData
for (int i = 0; i < jsonData[0][0].Count; i++)
{
Debug.Log(jsonData[0][i]["id"] + " " + jsonData[0][i]["name"] + " " + jsonData[0][i]["age"]);
}
运行结果
第二种,先写好接收Json的类,再用类的实例去接收,这种开始要蛮烦一点,但后面用起来就比较有条理
定义json数据的类
public class PlayerInfo {
public string id;
public string name;
public int age;
}
public class Root
{
public List<PlayerInfo> sites;
}
读取json
StreamReader streamreader = new StreamReader(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数据流
JsonReader jsonReader = new JsonReader(streamreader); //转换成json数据
Root root = JsonMapper.ToObject<Root>(jsonReader); //转化为jsonData
Debug.Log(root.sites.Count);
for (int i = 0; i < root.sites.Count; i++)
{
Debug.Log(root.sites[i].id + " " + root.sites[i].name + " " + root.sites[i].age);
}
另外,JsonMapper的ToObject方法可以读取三种类容JsonReader、TextReader和String,上面用的读是JsonReader的方法
public static JsonData ToObject(JsonReader reader);
public static JsonData ToObject(TextReader reader);
public static JsonData ToObject(string json);
public static T ToObject<T>(JsonReader reader);
public static T ToObject<T>(TextReader reader);
public static T ToObject<T>(string json);
来看看string读取(1)路径加载文本
//StreamReader streamreader = new StreamReader(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数据流
//JsonReader jsonReader = new JsonReader(streamreader); //转换成json数据
string jsonStr = File.ReadAllText(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数string
Root root = JsonMapper.ToObject<Root>(jsonStr); //转化为jsonData
Debug.Log(root.sites.Count);
for (int i = 0; i < root.sites.Count; i++)
{
Debug.Log(root.sites[i].id + " " + root.sites[i].name + " " + root.sites[i].age);
}
string读取(2)Resource加载文本
//StreamReader streamreader = new StreamReader(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数据流
//JsonReader jsonReader = new JsonReader(streamreader); //转换成json数据
//string jsonStr = File.ReadAllText(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数string
TextAsset textAsset = Resources.Load("Test") as TextAsset;
string jsonStr = textAsset.text;
Root root = JsonMapper.ToObject<Root>(jsonStr); //转化为jsonData
Debug.Log(root.sites.Count);
for (int i = 0; i < root.sites.Count; i++)
{
Debug.Log(root.sites[i].id + " " + root.sites[i].name + " " + root.sites[i].age);
}
TextReader读取
//StreamReader streamreader = new StreamReader(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数据流
//JsonReader jsonReader = new JsonReader(streamreader); //转换成json数据
//string jsonStr = File.ReadAllText(Application.dataPath + "/StreamingAssets/Test.txt"); //读取数据,转换成数string
//TextAsset textAsset = Resources.Load("Test") as TextAsset;
//string jsonStr = textAsset.text;
TextReader textReader = new StreamReader(Application.dataPath + "/StreamingAssets/Test.txt");
Root root = JsonMapper.ToObject<Root>(textReader); //转化为jsonData
Debug.Log(root.sites.Count);
for (int i = 0; i < root.sites.Count; i++)
{
Debug.Log(root.sites[i].id + " " + root.sites[i].name + " " + root.sites[i].age);
}
二、修改
/// <summary>
/// 修改Json文本
/// </summary>
/// <param name="url">json的路径</param>
/// <param name="str">要修改的json内容</param>
public static void SaveVer(string url, string str)
{
StreamWriter sw = new StreamWriter(url);
sw.Write(str);
sw.Close();
AssetDatabase.Refresh();
}