private void Start() { //简单Json信息的存取 //------------------ //appOfTrigger = new AppOfTrigger //{ // appNum = 3, // phoneState = true, // appList = new List<string>() // { // "抖音","BiliBili","刺激战场" // } //}; //SaveByJson(); //appOfTrigger = LoadJson(); //Debug.Log(appOfTrigger.appNum); //Debug.Log(appOfTrigger.phoneState); //foreach(var item in appOfTrigger.appList) //{ // Debug.Log(item); //} //复杂Json信息的存取 //------------------ //appOfTrigger = new AppOfTrigger //{ // appNum = 3, // phoneState = true, // appList = new List<AppProperty>() //}; //AppProperty appProperty = new AppProperty //{ // appName = "抖音", // triggerID = "Trigger", // triggerFavour = true, // useTimeList = new List<int> { 6, 7, 8 } //}; //appOfTrigger.appList.Add(appProperty); //SaveByJson(); appOfTrigger = LoadJson(); Debug.Log(appOfTrigger.appNum); Debug.Log(appOfTrigger.phoneState); foreach (var item in appOfTrigger.appList) { Debug.Log(item); Debug.Log(item.appName); Debug.Log(item.triggerFavour); Debug.Log(item.triggerID); foreach (var time in item.useTimeList) { Debug.Log(time); } } }
public AppOfTrigger LoadJson() { AppOfTrigger appItem = new AppOfTrigger(); string filaPath = Application.dataPath + "/Resources/Json/AppOfTrigger.json"; if (File.Exists(filaPath)) { StreamReader sr = new StreamReader(filaPath); string jsonStr = sr.ReadToEnd(); sr.Close(); appItem = JsonMapper.ToObject <AppOfTrigger>(jsonStr); } if (appItem == null) { Debug.Log("读取Json文件失败"); } return(appItem); }
//读取Json文件的方法 private AppOfTrigger LoadJson() { AppOfTrigger app = new AppOfTrigger(); string filePath = Application.dataPath + "/Resources/JSON" + "/AppOfTrigger.json"; if (File.Exists(filePath)) { StreamReader sr = new StreamReader(filePath); string jsonStr = sr.ReadToEnd(); sr.Close(); app = JsonMapper.ToObject <AppOfTrigger>(jsonStr); } if (app == null) { Debug.Log(filePath + " Json读取失败"); } return(app); }