Unity进阶第六章-Json和Xml

一、json

用系统的方式创建一个json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/*
 数据:格式
云天河 18 后羿射日 100 80 80
韩姜沙 17 顺手牵羊 900 11 23
josn:数组+字典:嵌套
{
"person":
[
    {
    "name":"云天河",
    "age":18
    },
    {
    "name":"韩菱沙",
    "age":17
        }
      ]
}


 */
[Serializable]
public class Person
{
    
    
    public string name;
    public int age;
}
public class Persons
{
    
    
    public Person[] persons;
}
public class PlayerState
{
    
    
    public int HP;
    public int score;
    public float dis;
    public int time;
}
public class JsonUtilityTest : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        //用系统的方式创建一个json
        Person person = new Person();
        person.name = "云天河";
        person.age = 18;
        Person person2 = new Person();
        person2.name = "韩菱沙";
        person2.age = 17;
        Persons persons = new Persons();
        persons.persons = new Person[] {
    
    person,person2};
        //转成json
        string jsonStr = JsonUtility.ToJson(persons);
        Debug.Log(jsonStr);
        //解析json
        Persons ps = JsonUtility.FromJson<Persons>(jsonStr);
        //遍历
        foreach(Person p in ps.persons)
        {
    
    
            Debug.Log(p.name);
        }
    }


}

引用dll文件解析

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
public class Hero
{
    
    
    public string name;
    public int age;
}
public class Heros
{
    
    
    public Hero[] heros;
}
public class LigJson : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        func1();
        func2();

        func3();
    }
    void func2()
    {
    
    
        //{"name":"超人","age":35}
        //jsonData 代表了json中的各种类型
        JsonData jd1 = new JsonData();
        jd1.SetJsonType(JsonType.Object);
        jd1["name"] = "超人";
        jd1["age"] = 35;
        Debug.Log(jd1.ToJson());
        JsonData jd2 = new JsonData();
        jd2.SetJsonType(JsonType.Object);
        jd2["name"] = "蝙蝠侠";
        jd2["age"] = 40;
        JsonData jds = new JsonData();
        jds.SetJsonType(JsonType.Array);
        jds.Add(jd1);
        jds.Add(jd2);
        JsonData heroJD = new JsonData();
        heroJD.SetJsonType(JsonType.Object);
        heroJD["heros"] = jds;
        Debug.Log(heroJD.ToJson());
    }

    // Update is called once per frame
    void func1()
    {
    
    
        //第一种创建和解析json的方法
        Hero hero1 = new Hero();
        hero1.name = "超人";
        hero1.age = 35;
        Hero hero2 = new Hero();
        hero2.name = "蝙蝠侠";
        hero2.age = 40;
        Heros heros = new Heros();
        heros.heros = new Hero[] {
    
     hero1, hero2 };
        string jsonStr = JsonMapper.ToJson(heros);
        Debug.Log(jsonStr);
        Heros hs = JsonMapper.ToObject<Heros>(jsonStr);
        Debug.Log(hs.heros[1].name);
    }
    void func3()
    {
    
    
        string jsonStr = "{'heros':[{'name':'超人','age':35},{'name':'蝙蝠侠','age':37}]}";
        JsonData herosJD = JsonMapper.ToObject(jsonStr);
        JsonData heros = herosJD["heros"];
        foreach(JsonData heroJd in heros)
        {
    
    
            Debug.Log(heroJd["name"].ToString()+" "+(int)heroJd["age"]);
        }
    }

}

二、XML

using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;

public class xml : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        /*
         超人 35 蝙蝠侠37 闪电侠 20
        节点:<name>value</name> 节点名称:name 节点值:value
        <name><p>22</p><name>
        属性:<name id="1">value</name> <name id="2">
         */
        //ParseXML();
        //ParseXML2();
        CreateXML();
    }
    //解析xml方法一
    // Update is called once per frame
    void ParseXML()
    {
    
    
        //xml文档类
        XmlDocument doc = new XmlDocument();
        //读取xml文件
        doc.Load(Application.dataPath + "/XML/test.xml");
        //解析
        XmlElement rootEle = doc.LastChild as XmlElement;
        //heros节点
        XmlElement herosEle = rootEle.FirstChild as XmlElement;
        //遍历hero节点
        foreach(XmlElement heroEle in herosEle.ChildNodes)
        {
    
    
            //获取id
            string id = heroEle.GetAttribute("id");
            //获取name
            string name = heroEle.ChildNodes[0].InnerText;
            //age
            string age = heroEle.ChildNodes[1].InnerText;
            Debug.Log(id + name + age);
        }
    }
    //解析xml方法二
    void ParseXML2()
    {
    
    
        //创建xml文档类
        XmlDocument doc = new XmlDocument();
        //读取xml
        doc.Load(Application.dataPath + "/XML/test.xml");
        /*
         Xpath 路径语法
        /root/heros/hero/name
        //name 相对路径
        //heros/hero[2]/name 获取第二个hero节点
        //heros/hero[last()-1]/name 获取倒数第二个
        //heros/hero[postion()<3]/name 获取前两个节点
         */
        XmlNodeList list = doc.SelectNodes("//heros/hero[@id]/name");
        foreach(XmlElement ele in list)
        {
    
    
            Debug.Log(ele.InnerText);
        }
    }
    //创建XML
    void CreateXML()
    {
    
    
        //创建文档类
        XmlDocument doc = new XmlDocument();
        //创建文档声明
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", "");
        doc.AppendChild(dec);
        //root节点
        XmlElement rootEle = doc.CreateElement("root");
        doc.AppendChild(rootEle);
        //heros节点
        XmlElement herosEle = doc.CreateElement("heros");
        rootEle.AppendChild(herosEle);
        //循环数据
        string[] names = new string[] {
    
     "超人","蝙蝠侠","闪电侠"};
        string[] ages = new string[] {
    
     "35", "37", "30" };
        for(int i = 0; i < 3; i++)
        {
    
    
            XmlElement heroEle = doc.CreateElement("hero");
            herosEle.AppendChild(heroEle);
            //name
            XmlElement nameEle = doc.CreateElement("name");
            nameEle.InnerText = names[i];
            heroEle.AppendChild(nameEle);
            //age节点
            XmlElement ageEle = doc.CreateElement("Age");
            ageEle.InnerText = ages[i];
            heroEle.AppendChild(ageEle);
            //添加属性
            heroEle.SetAttribute("id", i + "");
            XmlAttribute att = doc.CreateAttribute("id");
            att.Value = i + "";
            heroEle.Attributes.Append(att);
        }
        //保存xml文件
        doc.Save(Application.dataPath + "/XML/test2.xml");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43853077/article/details/125975176