Unity开发——3种Json插件及其使用简单介绍

25 篇文章 1 订阅
5 篇文章 0 订阅
1、LitJson插件
(1)介绍:

常用的Json插件

T datas = LitJson.JsonMapper.ToObject<T>(string value);

List<T> cityIds = LitJson.JsonMapper.ToObject<List<T>>(string s);

对于LitJson的详细使用说明,可查看本人的另一篇博文:

Unity数据存储——LitJson插件实现创建、存储、读取文件

(2)示例功能:读档
1)读取 Resources文件夹下的WeatherId.txt文件;
public static bool initDic = false;
    public static Dictionary<string,int> posToId = new Dictionary<string,int>();

    public static List<CityId> cityIds = new List<CityId>();

    public static int GetWeatherId(string cityName)
    {
        int id = 0;
        if (!initDic)
        {
            initDic = true;
            TextAsset ta = Resources.Load<TextAsset>("WeatherId");
            cityIds = LitJson.JsonMapper.ToObject<List<CityId>>(ta.text);
        }
        return id;
    }
2)读取 FilePath路径下的Json文件;
using System;
using UnityEngine;
using System.IO;

    public T ReadDatasJson<T>(string FilePath)
    {
        StreamReader sr = new StreamReader(FilePath);
        string ReadStr = sr.ReadToEnd();
        sr.Close();
        try
        {
            T fileDatas = LitJson.JsonMapper.ToObject<T>(ReadStr);
            return fileDatas;
        }
        catch (Exception)
        {
            Debug.LogError("本地设置文档格式错误,设置已重置");
            File.Delete(FilePath);
            return null;
        }
    }
2、Newtonsoft.Json
(1)介绍

C# .net的Json插件,Unity可用

string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(object value, Newtonsoft.Json.Formatting.Indented);

(2)示例功能:存档

将T类型里的数据转为string字符,并存到filePath路径下的json文件里,json格式保存;

using System.IO;

private void SaveDatasJson<T>(T value)
    {
        string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented);
        StreamWriter sw = new StreamWriter(filePath);
        sw.WriteLine(jsonStr);
        sw.Close();
    }
3、JsonUtility
(1)介绍:

Unity内置的Json插件,可从UnityAssetStore里获取;

List<T1> newMessages = JsonUtility.FromJson<T2>(string value).data;

List<T1> newMessages = JsonUtility.FromJson<List<T1>>(string value);

数据类型格式:T1,T2;

(2)示例功能:将从本地文件/服务器等获取到的字符,转化为目标格式
1)服务器/本地数据格式:
[Serializable]
public class T1
{
    public string Mes;
    public string Type;
}

[Serializable]
public class T2
{
    public bool success;//true
    public string message;//null
    public List<T1> data;
    /*[
     * {"Mes":"2021-01-19","Type":0}
     * {"Mes":"2028-03-29","Type":1}
     * {"Mes":"2011-11-22","Type":2}
     * ]
     */
}
2、解析从服务器获取的数据
    /// <summary>
    /// 隔一分钟获取一次数据
    /// </summary>
    /// <param name="tokenSource"></param>
    /// <returns></returns>
    public async Task GetWarnMessagesTask()
    {
        string _url = string.Concat(host, url);

        while (true)
        {
            //获取到数据后,把数据格式转化为目标格式
            string getData = HttpGet(_url);
            List<T1> newMessages = JsonUtility.FromJson<T2>(getData).data;

            ...
            ...
            ...

            //等60秒后重新获取数据
            await Task.Delay(TimeSpan.FromSeconds(60f));
        }
    }
4、比较
(1)使用要求

LitJson、JsonUtility相对Newtonsoft.Json来说,对数据参数格式要求更严格;

参数格式不同时,有可能出现缺少属性,找不到属性等报错;

比如;服务端有4个参数,脚本只写了三个所需的参数这种属性非一一对应情况

(2)兼容性:

Newtonsoft.Json兼容性相对更强些,格式转换,属性参数不一一对应时,会选择性跳过等;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值