c# unity 读取配置json文件
using System; using LitJson; string configStr; void Start() { ArrayList info = LoadFile(Application.dataPath,"config1.txt"); foreach (string strs in info) { configStr += strs; } // string str = @" // { // ""Speed"" : 2 // }"; JsonData jd = JsonMapper.ToObject(configStr); speed = (int)jd["Speed"];//2 } ArrayList LoadFile(string path,string name) { StreamReader sr = null; try{ string filePath=path+"//"+name; if (File.Exists(filePath)) { sr = File.OpenText(path + "//" + name); } else { return null; } } catch(Exception e) { return null; } string line; ArrayList arrList = new ArrayList(); while((line = sr.ReadLine()) != null) { arrList.Add(line); } sr.Close(); sr.Dispose(); return arrList; }
public void ResolveJson() { string str = @" { ""Name"" : ""yusong"", ""Age"" : 26, ""Birthday"" : ""1986-11-21"", ""Thumbnail"":[ { ""Url"": ""http://xuanyusong.com"", ""Height"": 256, ""Width"": ""200"" }, { ""Url"": ""http://baidu.com"", ""Height"": 1024, ""Width"": ""500"" } ] }"; JsonData jd = JsonMapper.ToObject(str); Debug.Log("name = " + (string)jd["Name"]); Debug.Log("Age = " + (int)jd["Age"]); Debug.Log("Birthday = " + (string)jd["Birthday"]); JsonData jdItems = jd["Thumbnail"]; for (int i = 0; i < jdItems.Count; i++) { Debug.Log("URL = " + jdItems[i]["Url"]); Debug.Log("Height = " + (int)jdItems[i]["Height"]); Debug.Log("Width = " + jdItems[i]["Width"]); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。