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; }
configTxt = (TextAsset)Resources.LoadAssetAtPath ("Assets/config.txt",typeof(TextAsset)); JsonData jd = JsonMapper.ToObject(configTxt.text);
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"]); } }