码迷,mamicode.com
首页 > Web开发 > 详细

c# unity 读取配置json文件

时间:2014-09-18 16:24:24      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   文件   

  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"]);   
        }
    }

 

c# unity 读取配置json文件

标签:style   blog   http   color   io   os   ar   for   文件   

原文地址:http://www.cnblogs.com/bkycjj/p/3979308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!