• 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;   
        }          
    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"]);   
            }
        }
  • 相关阅读:
    Java多线程
    http网页请求状态码
    C++文件读写
    算法训练 最大的算式
    算法训练 2的次幂表示
    线段树- 算法训练 操作格子
    Prim算法(最小生成树)
    Kruskal算法(最小生成树)
    Dijkstra算法(最短路)
    HDU5692 dfs + 线段树维护区间最大值
  • 原文地址:https://www.cnblogs.com/bkycjj/p/3979308.html
Copyright © 2020-2023  润新知