Excel转JSON

自己写的Excel转Json工具,详细内容,可以在Unity通用工具中进行查看

JosnUtility

这是Unity自己封装的一个Json封装工具包,可以通过将内存中的对象序列化为JSON格式的字符串,也可以将Json字符串反序列化为对象。

使用JsonUtility进行序列化

注意:

  1. float序列化看起来会有一点误差,但反序列化后是正确的数据

  2. 自定义类需要添加[System.Serializable] 特性

  3. 想要私有的变量也可以写入json文件中,可以为私有变量添加特性[SerializeField] 特性

  4. JsonUtility不支持字典

  5. 如果对象为空,JsonUtility不会把它设置为null,而是把对象全部设置为默认值

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

/// <summary>
/// 使用JsonUtility进行序列化  
/// 
/// 作者: DY
/// 创建日期: 2025-01-11
/// 版本: 1.0
/// </summary>
public class 使用JsonUtility进行序列化 : MonoBehaviour
{
    private void Start()
    {
        Player player = new Player();
        player.name = "龙骑吖";
        player.id = 0;
        player.speed = 13.6f;
        player.ints = new int[] { 1,2,3,4 };
        player.items = new List<Item>() { new Item(1, "剑"), new Item(2, "手机") };
        Dictionary<int,string> dic =  new Dictionary<int, string>();
        dic.Add(1, "zhagnsan");
        dic.Add(2, "lishi");
        player.friends = dic;

        // 序列化
        string json = JsonUtility.ToJson(player);
        File.WriteAllText(Path.Combine(Application.persistentDataPath, "player.json"),json);
        Debug.Log(Application.persistentDataPath);
    }

}

public class Player
{
    public string name;
    public int id;
    public float speed;
    public int[] ints;
    public List<Item> items;
    public Dictionary<int, string> friends;

    [SerializeField]
    private int private1;

    [SerializeField]
    protected int protected1;
}

[System.Serializable]
public class Item
{
    public int id;
    public string name;
    public Item(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
}

使用JosnUtility进行反序列化

注意:如果json中数据少了,读到内存中的数据也不会报错

using System.IO;
using UnityEngine;

/// <summary>
/// 使用JsonUtility进行反序列化  
/// 
/// 作者: DY
/// 创建日期: 2025-01-11
/// 版本: 1.0
/// </summary>
public class 使用JsonUtility进行反序列化 : MonoBehaviour
{
    private void Start()
    {
        string path = Path.Combine(Application.persistentDataPath, "player.json");
        string json = File.ReadAllText(path);
        Player player = JsonUtility.FromJson<Player>(json);
        print(player.ToString());
    }

}

JsonUtility的缺点

  1. JsonUtility无法直接读取数据集合

  2. 文本的编码格式必须是UTF-8,不然无法加载

LitJson

第三方库,很好的处理Json序列化和反序列化

获得LitJson

github中下载最新的LitJSON

https://github.com/LitJSON/litjson

使用LitJson序列化对象

注意:

  1. 相对于JsonUtility不需要加特性

  2. 不能序列化私有变量

  3. 支持字典

  4. ListJson可以准确保存null类型

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

/// <summary>
/// 使用LitJosn序列化对象  
/// 
/// 作者: DY
/// 创建日期: 2025-01-11
/// 版本: 1.0
/// </summary>
public class 使用LitJosn序列化对象 : MonoBehaviour
{

    private void Start()
    {
        Player2 player = new Player2();
        player.name = "龙骑吖";
        player.id = 0;
        player.speed = 13.6f;
        player.ints = new int[] { 1, 2, 3, 4 };
        player.items = new List<Item2>() { new Item2(1, "剑"), new Item2(2, "手机") };
        Dictionary<int, string> dic = new Dictionary<int, string>();
        dic.Add(1, "zhagnsan");
        dic.Add(2, "lishi");
        player.friends = dic;

        // 进行序列化
        string json = JsonMapper.ToJson(player);
        string path = Path.Combine(Application.persistentDataPath,"player2.json");
        File.WriteAllText(path, json);
        Debug.Log(Application.persistentDataPath);
    }
}

public class Player2
{
    public string name;
    public int id;
    public float speed;
    public int[] ints;
    public List<Item2> items;
    public Dictionary<int, string> friends;
}

public class Item2
{
    public int id;
    public string name;
    public Item2(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
}

使用LitJson反序列化对象

注意:

  1. LitJson不支持字典的键是int类型的,要存就存字符串类型的

  2. 自定义类必须要有无参构造

我们需要对之前的数据进行修改

public class Player2
{
    // 1. 将之间代码中的字典的键改为string类型
    public Dictionary<string, string> friends;
}
public class Item2
{
    // 2. 添加无参构造器,不然会报错
    public Item2()
    {

    }
}

using LitJson;
using System.IO;
using UnityEngine;
/// <summary>
/// 使用LitJson反序列化  
/// 
/// 作者: DY
/// 创建日期: 2025-01-11
/// 版本: 1.0
/// </summary>
public class 使用LitJson反序列化 : MonoBehaviour
{
    private void Start()
    {
        string path = Path.Combine(Application.persistentDataPath, "player2.json");
        string json = File.ReadAllText(path);
        // 第一种方式
        Player2 player2 = JsonMapper.ToObject<Player2>(json);

        // 第二种方式
        JsonData player3 = JsonMapper.ToObject<JsonData>(json);
        print(player3["name"]);
        print(player3["speed"]);
    }
}

LitJson直接读取集合结构

读取数组,假设我们文件中有下面的数据

[{"id":1,"name":"tank1"},
{"id":2,"name":"tank2"},
{"id":3,"name":"tank3"}]

我们可以直接读取

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

/// <summary>
/// LitJson直接读取集合结构  
/// 
/// 作者: DY
/// 创建日期: 2025-01-11
/// 版本: 1.0
/// </summary>
public class LitJson直接读取集合结构 : MonoBehaviour
{
    private void Start()
    {
        // 准备数据
        //List<Tank> list = new List<Tank>() { new Tank(1, "tank1"), new Tank(2, "tank2"), new Tank(3,"tank3") };
        //string json = JsonMapper.ToJson(list);
        //File.WriteAllText(Path.Combine(Application.streamingAssetsPath, "tanks.json"), json);
        string json = File.ReadAllText(Path.Combine(Application.streamingAssetsPath, "tanks.json"));
        List<Tank> tanks = JsonMapper.ToObject<List<Tank>>(json);
    }

}

public class Tank
{
    public int id;
    public string name;

    public Tank()
    {

    }

    public Tank(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
}