Unity学习笔记--数据持久化Json

JSON相关

json是国际通用语言,可以跨平台(游戏,软件,网页,不同OS)使用,

json语法较为简单,使用更广泛。json使用键值对来存储。

image-20231201174754215

认识json文件

//注意字典类型存储时,键是以string类型存储的 需要添加“”

{
  "name": "TonyChang",
  "age":21,
  "sex":true,
  "Float": 2.5,
  "arrarys":[1,5,9],
  "friends": [{"name": "Tom","age":21, "sex":true,"Float": 2.8},
    {"name": "Peter","age":17, "sex":true,"Float": 3.5},
    {"name": "Jack","age":25, "sex":true,"Float": 5.0}
  ],
  "university": {"address": "唐山","province":"河北"},
  "dic": {"1":"125","2": 911}, 
  "son": null
}

Excel转换为JSON文件:

使用网站来转换:bejson

挖坑-----》开发一个工具,使各种类型存储文件进行转换。

Json的读写:

  1. jsonUtlity中的使用:

    string jisonStr = JsonUtility.ToJson(_writer);
    File.WriteAllText(Application.persistentDataPath+"/DemoJson1.json",jisonStr);
    

    转储为JSON时要点:

    1. float存储时看起来会有一些误差
    2. 自定义类序列化要添加序列化特性[System.Serializable]
    3. 想要序列化私有变量,需要添加序列化特性[SerializeField]
    4. JsonUtility不支持字典
    5. JsonUtility存储对象时候不会为null是默认值的数值

完整的类

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

namespace Building.JSON
{
    public class Writer
    {
        public string name;
        public int age;
        public bool sex;
        [SerializeField]
        protected float numberF = 3.15f;
        [SerializeField]
        private double numberD = 6.15;
        public List<int> Array;
        public IDCard idcard;
    }

    [Serializable]
    public class IDCard
    {
        public int stu_id;
        public int cl_id;
    }
    public class JsonPractice:MonoBehaviour
    {
        private Writer _writer;

        private void Awake()
        {
            _writer = new Writer();
            _writer.name = "TonyCode";
            _writer.sex = true;
            _writer.age = 21;
            _writer.Array = new List<int>() {1, 2, 3, 4, 5};
            _writer.idcard = new IDCard();
            _writer.idcard.cl_id = 256;
            _writer.idcard.cl_id = 206;
            string jisonStr = JsonUtility.ToJson(_writer);
            File.WriteAllText(Application.persistentDataPath+"/DemoJson1.json",jisonStr);
            print(Application.persistentDataPath);
        }
    }
}

image-20231202192918820

存储生成的json文件内容。

反序列化:

string jsonStrRead = File.ReadAllText(Application.persistentDataPath + "/DemoJson1.json");
//反序列化 
Writer writer01 = JsonUtility.FromJson<Writer>(jsonStrRead);
Writer writer02=JsonUtility.FromJson(jsonStrRead,typeof(Writer)) as Writer;

注意:使用JsonUtility.FromJson进行反序列化时候接受对象是一个对象,不能是一个数据集合来接受Json中存储的数据内容。并且json的编码格式必须为UTF-8。

JSON文件的写入与读取,本质是对text文本的读写,所以会调用File类中对text文本相关的方法。

先将类转换为string字符串(Json文件格式的字符串),然后调用文件方法进行读写。

2.LitJson(第三方开发的工具包)

 //使用LitJson存储
 string jsonStr2=JsonMapper.ToJson(_writer);
 File.WriteAllText(Application.persistentDataPath+"/DemoJson2.json",jsonStr);

注意:(区别JsonUtility)

  • 不能序列化private变量;
  • 对于自定义类不需要添加序列化特性就可以进行序列化;
  • 支持字典类型,建议字典类型的键的类型为“string”;
  • 可以准确保存null类型;
//使用LitJson读(反序列化)
string jsonStrRead3 = File.ReadAllText(Application.persistentDataPath + "/DemoJson2.json");
//方法一
JsonData data = JsonMapper.ToObject(jsonStrRead3);
print(data["name"]);
//方法二 (使用较多)
Writer writer03= JsonMapper.ToObject<Writer>(jsonStrRead3);

读取数据集合:

//数据集合中元素类型
public class RoleInfo
{
    public int hp;
    public int speed;
    public int volume;
    public string resName;
    public int scale;
}
//读取数据集合
 string jsonStrRoleList = File.ReadAllText(Application.persistentDataPath + "/RoleInfo.json");
 List<RoleInfo> roleInfos = JsonMapper.ToObject<List<RoleInfo>>(jsonStrRoleList);
 foreach (var roleInfo in roleInfos )
 {
 	print(roleInfo.hp+"   "+roleInfo.speed+"   "+roleInfo.resName);
 }

RoleInfo.json内容

[
{"hp":4,"speed":6,"volume":5,"resName":"Airplane/Airplane1","scale":15},
{"hp":3,"speed":7,"volume":4,"resName":"Airplane/Airplane2","scale":15},
{"hp":2,"speed":8,"volume":3,"resName":"Airplane/Airplane3","scale":15},
{"hp":10,"speed":3,"volume":10,"resName":"Airplane/Airplane4","scale":6},
{"hp":6,"speed":5,"volume":7,"resName":"Airplane/Airplane5","scale":10}
]

读取字典:

 //读取字典
 //注意字典元素内容最后一个不应当添加逗号
 string jsonStrDic = File.ReadAllText(Application.persistentDataPath + "/JsonDic.json");
 Dictionary<string, int> games = JsonMapper.ToObject<Dictionary<string, int>>(jsonStrDic);
 foreach (KeyValuePair<string,int> kv in games)
 {
	 print(kv.Key+" "+kv.Value);
 }

JsonDic.json中内容:

{
   "Tony": 100,
   "Jack": 125,
   "Pony": 156,
   "Tom": 126
}

注意:

  1. litJson支持字典存储,字典键的类型为string
  2. 自定义类结构一定要有无参构造函数
  3. LitJson可以直接读取数据集合
  4. LitJson读取字典元素时候 字典内容最后一个不可以加逗号(可能反序列化失败)

对比JsonUtility和JsonLit二者使用:

相同:

  1. 二者都是对json文件进行序列化与反序列化的工具类
  2. 二者都是静态调用,使用其中的方法
  3. json的格式必须为UTF-8格式

区别:LitJson对比JsonUtility

  1. 对于空对象,LitJson对可以存储null类型,而后者只存储对应的默认数据数据值
  2. LItJson支持数据集合的读取,而后者需要将其转换为对象内部的数组才可以读取,读取存储结果为对象类型,而非数据集类型。
  3. LitJson支持对字典的存取,字典的键的类型为string
  4. LitJson存储兹定于数据类型时候无需添加序列化特性,而后者需要
  5. LitJson不支持对priivate数据内容的序列化,后者使用时候添加序列化特性则可以完成存储
  6. LitJson要求自定义数据类型必须有无参构造,而JsonUtility则不需要

Json读存的工具类:

using System.IO;
using LitJson;
using UnityEngine;

namespace Building.JSON
{
    public enum SaveJsonType
    {
        JsonUtility,
        LitJson
    }
    public class JsonManager
    {
        private static JsonManager instance=new JsonManager();
        public static JsonManager Instance => instance;

        private JsonManager()
        {
            
        }
        /// <summary>
        ///  存储Json方法
        /// </summary>
        /// <param name="data">要存储的数据类</param>
        /// <param name="FileName">存储文件名称</param>
        /// <param name="type">存储方法</param>
        public void SaveJson(object data, string FileName,SaveJsonType type=SaveJsonType.LitJson)
        {
            //确定路径
            string path = Application.persistentDataPath + "/"+FileName+ ".json";
            string jsonStr="";
            switch (type)
            {
                case SaveJsonType.LitJson:
                    jsonStr = JsonMapper.ToJson(data);
                    break;
                case SaveJsonType.JsonUtility:
                    jsonStr = JsonUtility.ToJson(data);
                    break;
            }
           //写入文件
           File.WriteAllText(path,jsonStr);
        }
        
        /// <summary>
        /// 读取json方法
        /// </summary>
        /// <param name="FileName">读取json的名称</param>
        /// <param name="type">读取方式</param>
        /// <typeparam name="T">读取json文件中数据类型</typeparam>
        /// <returns></returns>
        public T LoadJson<T>(string FileName, SaveJsonType type = SaveJsonType.LitJson) where T : new()
        {
            //确定路径
            string path = Application.persistentDataPath + "/"+FileName+ ".json";
            if (!File.Exists(path))
            {
                path=Application.streamingAssetsPath+"/"+FileName+".json";
                if (!File.Exists(path))
                {
                    return new T();
                }
            }
            string jsonStrRead=File.ReadAllText(path);
            T data = default(T);
            switch (type)
            {
                case SaveJsonType.LitJson:
                    data=JsonMapper.ToObject<T>(jsonStrRead);
                    break;
                case SaveJsonType.JsonUtility:
                    data= JsonUtility.FromJson<T>(jsonStrRead);
                    break;
            }
            return data;
        }
    }
}

posted @ 2023-12-03 12:22  畅知  阅读(124)  评论(0编辑  收藏  举报