【Unity数据持久化_Json】(三)C#读取存储Json文件-Jsonutility是什么?Jsonutility序列化和反序列化

一、Jsonutility是什么?

Jsonutility是Unity自带的用于解析Json的公共类
它可以:
1.将内存中对象序列化为Json格式的字符串
2.将Json字符串反序列化为类对象

二、补充:在文件中存读字符串

        //1.存储字符串到指定文件中
        //  参数1 文件路径
        //  参数2 要写入的内容
        File.WriteAllText(Application.persistentDataPath + "/Test.json", "我被存进去啦");

        //2.读取指定路径文件中的字符串
        //  参数1 文件路径
        string str = File.ReadAllText(Application.persistentDataPath +  "/Test.json");

三、使用Jsonutility进行序列化

序列化就是把内存中的数据存储到硬盘上
API:JsonUtility.ToJson(对象);

注意:
1.float被序列化后看起来会有一些误差,读取后值是正确的
2.自定义类需要加上序列化特性[System.Serializable]
   最外层的类前不需要加,如果它内部还包含了别的类 就需要加了
3.想要序列化private和protected变量,需要加特性[SerializeField]
4.JsonUtility不支持序列化字典
5.JsonUtility存储null对象不会是null,而是默认值的数据

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
//准备数据结构类
public class PlayerInfo2
{
    
    
    public string name;
    public int age;
    public bool sex;
    public float speed;
    public int[] ids;
    public List<int> ids2;
    public Dictionary<int, string> dic;
    public Item item1;
    public List<Item> item2;
    //想要序列化私有变量,需要加此特性
    [SerializeField]
    private int testPrivate = 1;
    [SerializeField]
    protected int testProtected = 2;
}
//想要序列化自定义类 前面要加此特性
[System.Serializable]
public class Item
{
    
    
    public int id;
    public int num;
    public Item(int id, int num)
    {
    
    
        this.id = id;
        this.num = num;
    }
}
public class Lesson1 : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        //声明对象 并初始化数据
        PlayerInfo2 p = new PlayerInfo2();
        p.name = "假面骑士1号";
        p.age = 24;
        p.sex = true;
        p.speed = 56.7f;
        p.ids = new int[] {
    
     1, 2, 3 };
        p.ids2 = new List<int>() {
    
     4, 5, 6 };
        p.dic = new Dictionary<int, string>() {
    
     {
    
     1, "啊" }, {
    
     2,"哈" } };
        p.item1 = new Item(1, 10);
        p.item2 = new List<Item>() {
    
     new Item(2, 4), new Item(3, 99) };
        //序列化此对象
        //(把类对象序列化为了Json字符串)
        string jsonStr = JsonUtility.ToJson(p);
        //把序列化后的字符串存到本地
        File.WriteAllText(Application.persistentDataPath + "/PlayerInfoJson.json",  jsonStr);
    }
}

保存到本地的Json文件:
在这里插入图片描述

四、使用Jsonutility进行反序列化

反序列化就是把硬盘上的数据读取到内存中的对象
API:JsonUtility.FromJson(字符串);

注意:
如果Json中的数据少了,读取到内存中的类对象时不会报错,而是赋默认值

        //读取文件中的Json字符串
        string jsonStr = File.ReadAllText(Application.persistentDataPath +  "/PlayerInfoJson.json");
        //Json字符串数据读取到对象
        PlayerInfo2 p2 = JsonUtility.FromJson<PlayerInfo2>(jsonStr);

五、注意事项

1.JsonUtility无法直接读取数据集合
这就是JsonUtility不方便的地方,将在LitJson中改善
如下图,这是通过Excel转换的Json数据
在这里插入图片描述
如果依然想读取,就把它改写成:
{}中有个对象,里面有个变量名为list的数组,数组中存储的就是这些数据
在这里插入图片描述

//在外层再包裹一个类RoleData,通过RoleData去读取他
public class RoleData
{
    
    
    public List<RoleInfo> list;
}
[System.Serializable]
public class RoleInfo
{
    
    
    public int hp;
    public int speed;
    public string resName;
    public int scale;
}
public class Lesson1 : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        string jsonStr = File.ReadAllText(Application.streamingAssetsPath +  "/RoleInfo.json");
        //读取
        RoleData data = JsonUtility.FromJson<RoleData>(jsonStr);
    }
}

在这里插入图片描述
2.文档编码格式必须是UTF-8,否则无法加载

猜你喜欢

转载自blog.csdn.net/SEA_0825/article/details/127253983
今日推荐