zoukankan      html  css  js  c++  java
  • Unity3d-XML文件数据解析&JSON数据解析

    1.XML文件数据解析:(首先须要导入XMLParser解析器,The latest released download from:http://dev.grumpyferret.com/unity/)


    XML文件内容:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

    <ROOT>

    <table wave="1" enemyname="ground" level="1" wait="3"/>

    ...

    </ROOT>


    //XML数据模型类

    public class SpawnData
    {
    public int wave = 1;
    public string enemyname = "";
    public int level = 1;
    public float wait = 1.0f;
    }

    //读取XML文件

    //存储XML文件
    public TextAsset xmldata;
    //保存读出数据
    ArrayList m_enemylist;
    void ReadXML()
    {
            m_enemylist = new ArrayList();
            XMLParser xmlparse = new XMLParser();
            XMLNode node = xmlparse.Parse(xmldata.text);
            XMLNodeList list = node.GetNodeList("ROOT>0>table");
           for (int i = 0; i < list.Count; i++)
          {
            string wave = node.GetValue("ROOT>0>table>" + i + ">@wave");
            string enemyname = node.GetValue("ROOT>0>table>" + i + ">@enemyname");
            string level = node.GetValue("ROOT>0>table>" + i + ">@level");
            string wait = node.GetValue("ROOT>0>table>" + i + ">@wait");
    
            SpawnData data = new SpawnData();
            data.wave = int.Parse(wave);
            data.enemyname = enemyname;
            data.level = int.Parse(level);
            data.wait = float.Parse(wait);
    
            m_enemylist.Add(data);
          }
    }

    2.JSON数据解析(首先须要导入工具LitJSON,下载地址:http://lbv.github.io/litjson//)

    1>放到Unityproject中删除目录benchmarks和test。在须要数据解析的代码文件里增加:using LitJson;

    2>代码解析:

    JSON文件数据例如以下:

    {"name":"中国",
    "provinces":[

    {"name":"黑龙江","id":1,"citys":["佳木斯","大庆"]},

    {"name":"辽宁","id":2,"citys":["沈阳","大连","齐齐哈尔"]},

    {"name":"吉林","id":3,"citys":["吉林市","长春市","四平市","哈尔滨"]}

    ]}

    建类:

        public class Country
        {
            public string name {get; set;}
            public Province [] provinces;
        }
    
        public class Province
        {
            public string name;
            public int id;
            public string[] cities;
        }
    
        public TextAsset jsondata;//拖入JSON.txt文件
        void ReadJSON()
        {
            JsonData jd = JsonMapper.ToObject(jsondata.text);
            print("p.name" + (string)jd["name"]);
            JsonData provinces = jd["provinces"];
    
            for(int i = 0; i < provinces.Count; i++)
            {
                print("j.name" + provinces[i]["name"]);
                print("j.cities" + provinces[i]["citys"].Count);
                for(int j = 0; j < provinces[i]["citys"].Count; j++)
                {
                    print("provinces[i][citys][" + j + "]" + provinces[i]["citys"][j]);
                }
            }
        }

    执行结果例如以下图:



  • 相关阅读:
    elasticsearch 中的Multi Match Query
    activiti 流程部署的各种方式
    elasticsearch 嵌套对象之嵌套类型
    elasticsearch Java High Level REST 相关操作封装
    elasticsearch 字段数据类型
    ubuntu 安装 docker
    elasticsearch 通过HTTP RESTful API 操作数据
    facenet 人脸识别(二)——创建人脸库搭建人脸识别系统
    POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)
    墨卡托投影, GPS 坐标转像素, GPS 坐标转距离
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6805890.html
Copyright © 2011-2022 走看看