LitJson
這個庫需要找資源,找到LitJson.dll后將它放在Assets文件夾下,在腳本中使用using引入即可
測試代碼
json文件:
{"Archice":[{"EXP":700,"HP":800,"Level":4,"MapID":2,"MissionCount":0,"MissionID":5,"ScriptSign":"0000001111"}]}
這個Json文件是一個存檔文件,鍵"Archice"對應的值是一個數組,這個數組儲存每一個存檔數據,每個存檔數據有EXP,HP,Level,MapID, MissionCount, ScriptSign這幾個鍵,在Unity中要儲存這個Json文件里的數據的話,要使用
Dictionary<string, List<Dictionary<string, object>>>
這個類型來儲存,顯然,鍵值關系使用Dictionary, 數組關系使用List存儲
測試:
//讀取數據
public void ReadData()
{
string FileName = "Assets/Data/Archice.json";
StreamReader json = File.OpenText(FileName);
string input = json.ReadToEnd();
Dictionary<string, List<Dictionary<string, object>>> jsonObject = JsonMapper.ToObject<Dictionary<string, List<Dictionary<string, object>>>>(input);
Debug.Log(jsonObject["Archice"][0]["EXP"]);
}
這段代碼將第一個存檔中的經驗值讀取出來
隨便在某個地方執行ReadData函數
結果:
解析成功