Unity存档--Json

  1. 认识Json:Json是一门数据描述语言,它只是一种数据的格式,单机游戏一般是通过写Json文件操作,也就是写文件的方式存在本地电脑中,联网游戏一般就是将它放在数据库中

  1. 了解Json的书写格式:假比现在有人物

名字:李逍遥 年龄:18 武功:万雷诀,

名字:赵灵儿 年龄:14 武功:御剑术

这两个人物分别用Json的格式表达出来就是:

{

"Name":"李逍遥",

"Age":18,

"Kungfu":"万雷诀"

}

{

"Name":"赵灵儿",

"Age":14,

"Kungfu":"御剑术"

}

这里我们来看一个c#的类

class Person

{

public string Name;

public string Age;

public string Kungfu;

}

Person p1=new Person();

p1.Name="李逍遥";

p1.Age=18;

p1.Kungfu="万雷诀";

这时可以看出p1这个对象就可以用Json这种格式储存下来

Json格式不仅可以描述类的对象({}),还可以描述数组([])

比如[1,2,3,4,5]这在c#中就是int[]数组,["李逍遥","赵灵儿"]这在c#中就是string[]数组

复杂一点的例如:

[

{

"Name":"李逍遥",

"Age":18,

"Kungfu":"万雷诀"

}

{

"Name":"赵灵儿",

"Age":14,

"Kungfu":"御剑术"

}

]

这个其实就是[{},{}]是这种格式,也就可以看出在c#中是存有Person对象的数组,也就是Person[]

再比如这样:

{

“personArry”:

[

{

"Name":"李逍遥",

"Age":18,

"Kungfu":"万雷诀"

}

{

"Name":"赵灵儿",

"Age":14,

"Kungfu":"御剑术"

}

]

}

这个看着挺复杂的但是看它的结构是这样的{ [{},{}] },{}可以理解为类的对象,[]理解为数组,那么这里再c#中理解为,有两个类,最外层的{}是一个类,这个类中有一个名叫personArry数组,这个personArry数组是用来存放Person这个类的对象的,且存放了两个对象,就像这样

class Persons(类名)

{

Public Person personArry[];

}

了解了Json的基本格式后,就学习如何在unity中使用,目前有两种方法使用,第一种就是JsonUtility,第二种就是LitJson

在使用Json都会涉及到两个过程,一个创建,一个解析

首先先创建了空对象,在空对象上挂载了测试JsonUtility的脚本,后面的LitJson同理

  1. JsonUnility:

(1)创建Json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;//想要使用序列化就需要引入这个命名空间

[Serializable]//需要在转换为json格式的类的上方添加序列化
public class Person
{
    public string name;
    public int age;
}

public class JsonUtilityDemo : MonoBehaviour
{   
    void Start()
    {
        Person person = new Person();
        person.name = "李逍遥";
        person.age = 18;
        //转成json字符串
        string jsonStr = JsonUtility.ToJson(person);
        Debug.Log(jsonStr);
    }
}

控制台输出的结果为:

再做复杂一点的结构比如{ [{},{}] }

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

[Serializable]
public class Person
{
    public string name;
    public int age;
}

[Serializable]
public class Persons
{
    public Person[] personArry;
}
public class JsonUtilityDemo : MonoBehaviour
{
    
    void Start()
    {
        Person person = new Person();
        person.name = "李逍遥";
        person.age = 18;
        //转成json字符串
        string jsonStr = JsonUtility.ToJson(person);

        Person person2 = new Person();
        person2.name = "赵灵儿";
        person2.age = 14;

        Persons persons = new Persons();
        persons.personArry = new Person[] { person, person2 };
        string jsonstr2 = JsonUtility.ToJson(persons);
        Debug.Log(jsonstr2);
    }
}

(2)解析Json:

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

[Serializable]
public class Person
{
    public string name;
    public int age;
}

[Serializable]
public class Persons
{
    public Person[] personArry;
}
public class JsonUtilityDemo : MonoBehaviour
{
    
    void Start()
    {
        Person person = new Person();
        person.name = "李逍遥";
        person.age = 18;
        //转成json字符串
        string jsonStr = JsonUtility.ToJson(person);

        Person person2 = new Person();
        person2.name = "赵灵儿";
        person2.age = 14;

        Persons persons = new Persons();
        persons.personArry = new Person[] { person, person2 };
        string jsonstr2 = JsonUtility.ToJson(persons);
        //Debug.Log(jsonstr2);

        //解析
        Persons ps = JsonUtility.FromJson<Persons>(jsonstr2);//这里的类是依据最外层{}决定的
        foreach(Person p in ps.personArry)
        {
            Debug.Log(p.name);
        }
    }
}

  1. LitJson:LitJson的创建和解析有两种方案

因为LitJson是第三方插件,需要自己下载litjson的dll文件,并最好在Assets中创建名叫Plugins的文件夹,并把litjson的文件拖过去即可。

方案(一):(1)创建Json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//记得引入litjson的命名空间

public class Hero
{
    public string name;
    public int age;
}

public class Heros
{
    public Hero[] HeroArry;
}

public class litJson : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        func1();
    }

    //第一种创建和解析的方法
   void func1()
    {
        Hero hero1 = new Hero();
        hero1.name = "超人";
        hero1.age = 23;

        Hero hero2 = new Hero();
        hero2.name = "蝙蝠侠";
        hero2.age = 42;

        Heros heros = new Heros();
        heros.HeroArry = new Hero[] { hero1, hero2 };
        
        //创建Json
        string jsonStr = JsonMapper.ToJson(heros);
        //这样的结构就是{ [{},{}] }
        Debug.Log(jsonStr);
    }
}

(2)解析Json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//记得引入litjson的命名空间

public class Hero
{
    public string name;
    public int age;
}

public class Heros
{
    public Hero[] HeroArry;
}

public class litJson : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        func1();
    }

    //第一种创建和解析的方法
   void func1()
    {
        Hero hero1 = new Hero();
        hero1.name = "超人";
        hero1.age = 23;

        Hero hero2 = new Hero();
        hero2.name = "蝙蝠侠";
        hero2.age = 42;

        Heros heros = new Heros();
        heros.HeroArry = new Hero[] { hero1, hero2 };
        
        //创建Json
        string jsonStr = JsonMapper.ToJson(heros);
        //这样的结构就是{ [{},{}] }
        //Debug.Log(jsonStr);

        //解析Json
        Heros hs = JsonMapper.ToObject<Heros>(jsonStr);
        Debug.Log(hs.HeroArry[0].name);
    }
}

方案(二):(1)创建Json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//记得引入litjson的命名空间

public class Hero
{
    public string name;
    public int age;
}

public class Heros
{
    public Hero[] HeroArry;
}

public class litJson : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        func2();
    }


    //第二种方案创建Json
    void func2()//也可以不用创建类来创建Json
    {
        //{"name":"超人","power";23}
        JsonData jd = new JsonData();//这个JsonData可以代表类{}或者数组[]
        //jd.SetJsonType(JsonType.Object);//可以像这样设置JsonData的类型,不过也可以直接使用,例如
        jd["name"] = "超人";
        jd["power"] = 90;
        Debug.Log(jd.ToJson());

    }
}

创建更复杂的结构{ [{},{}] }

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//记得引入litjson的命名空间

public class Hero
{
    public string name;
    public int age;
}

public class Heros
{
    public Hero[] HeroArry;
}

public class litJson : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        func2();
    }

    //第二种方案创建Json
    void func2()//也可以不用创建类来创建Json
    {
        //{"name":"超人","power";23}
        //JsonData jd = new JsonData();//这个JsonData可以代表类{}或者数组[]
        //jd.SetJsonType(JsonType.Object);//可以像这样设置JsonData的类型,不过也可以直接使用,例如
        // jd["name"] = "超人";
        // jd["power"] = 90;
        //Debug.Log(jd.ToJson());

        //这样的结构{[{},{}]}
        JsonData herosJD = new JsonData();//这是最外层的{}
        JsonData hero1JD = new JsonData();//这是中间的两个{}
        hero1JD["name"] = "超人";
        hero1JD["power"] = 90;
        JsonData hero2JD = new JsonData();
        hero2JD["name"] = "蝙蝠侠";
        hero2JD["power"] = 45;

        JsonData heroArry = new JsonData();//这个是[]
        heroArry.Add(hero1JD);//这是将两个{}对象放在数组中
        heroArry.Add(hero2JD);

        herosJD["heroArry"]= heroArry;//这一步将数组放在最外层的{}中
        Debug.Log(herosJD.ToJson());

    }
}

(2)解析Json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;//记得引入litjson的命名空间

public class Hero
{
    public string name;
    public int age;
}

public class Heros
{
    public Hero[] HeroArry;
}

public class litJson : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        func3();
    }

    //第二种方案解析Json
    void func3()
    {
        string jsonStr = "{'heroArry':[{'name':'超人','power':90},{'name':'蝙蝠侠','power':53}]}";
        JsonData herosJD = JsonMapper.ToObject(jsonStr);//最外层的{}
        JsonData heroArry = herosJD["heroArry"];//[]
        foreach(JsonData heroJD in heroArry)
        {
            Debug.Log(heroJD["name"].ToString());
        }
    }
}

创建Json就是类转换为Json的过程,解析Json就是将Json转换为类的过程

猜你喜欢

转载自blog.csdn.net/qq_62947569/article/details/129101939