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"]);
        }
    }
时间: 2024-08-30 11:15:08

c# unity 读取配置json文件的相关文章

DNC读取配置Json文件到类中并在程序使用

ConfigurationBuilder 这个类提供了配置绑定,在dnc中 Program中WebHost提供了默认的绑定(appsettings文件) 如果我们需要加载我们自己的json配置文件怎么处理 var builder = new ConfigurationBuilder(); 这里builder 提供了很多添加的方式 1.第一种:直接添加json文件路径,这里需要注意的json文件地址问题 builder.AddJsonFile("path").Build(); 2.第二种

iOS国家城市选择器 读取本地json文件

最近在做的产品有这么个需求,读取本地json文件中的国家和城市信息,显示到pickerview上,在网上查了一下,发现没有什么合适的可用资源,所以就自己写了一个简单的DEMO. 效果图: 读取本地json的方法如下: + (NSMutableArray *)getCityData { NSArray *jsonArray = [[NSArray alloc]init]; NSData *fileData = [[NSData alloc]init]; NSUserDefaults *UD = [

C++简单使用Jsoncpp来读取写入json文件

一.源码编译 C++操作json字符串最好的库应该就是jsoncpp了,开源并且跨平台.它可以从这里下载. 下载后将其解压到任意目录,它默认提供VS2003和VS2010的工程文件,使用VS2010可以直接打开makefiles\msvc2010目录下的sln文件. 工程文件提供Jsoncpp的win32和win64静态库生成.点击生成--批生成选择需要生成的配置后即可生成jsoncpp静态库.生成的文件在makefiles\msvc2010\(x64\)Debug(Release)\目录下.

jQuery中读取本地json文件

json文件是一种轻量级的数据交互格式.一般在jquery中使用getJSON()方法读取,具体示例代码如下,感兴趣的朋友可以参考下哈,希望可以帮助到你 $.getJSON(url,[data],[callback]) url:加载的页面地址 data: 可选项,发送到服务器的数据,格式是key/value callback:可选项,加载成功后执行的回调函数 目录结构: 1.首先建一个JSON格式的文件userinfo.json 保存用户信息. 如下: [ { "name":"

读取本地Json文件

//读取Json文件  地区 //将文件拖到本地  获取json数据 //获取json文件路径 NSString *pathArea=[[NSBundle mainBundle] pathForResource:@"area" ofType:@"json"]; //==Json数据 NSData *dataArea=[NSData dataWithContentsOfFile:pathArea]; //==JsonObject //将data 转换成json数据 N

jQuery ajax读取本地json文件

json文件 { "first":[ {"name":"张三","sex":"男"}, {"name":"李四","sex":"男"}, {"name":"王武","sex":"男"}, {"name":"李梅"

php读取修改json文件

//判断文件是否存在 1 <?php 2 //判断文件是否存在 3 if(file_exists("login/userinfo.json")){ 4 echo "文件存在"; 5 } 6 else{ 7 echo "文件不存在"; 8 } 9 $filename="login/userinfo.json"; //取得json文件的属性值 1 $js=file_get_contents($filename); 2 $js

js 读取本地Json 文件

注意:把json 文件当做js 引用到页面上(city.js) <script src="zepto.min.js"></script>    <script src="city.js"></script>    <script type="text/javascript">        $(function () {            $.getJSON("city.j

Unity读取Android SDcard文件

一.添加权限 权限添加 :Player settings -- Other settings -- write permission的设置 Sdcard.这个是在Unity编辑器里打包的情况. 如果导出到studio 里面的话,可自行修改Manifest文件. 二.两种方式 IO方式 加载sdcard上的图片资源 加载的  /storage/emulated/0/ProjectName/image.jpg, image = this.GetComponentInChildren<Image>(