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);
}
好了,差不多就是這樣