您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

243 行
7.7 KiB

using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
namespace Metacity.Publish.Editor
{
/// <summary>
/// JSON对象工具类
/// <para>用于序列化和反序列化JSON字符串</para>
/// </summary>
public static class JsonTools
{
/// <summary>
/// 加载JSON文件
/// </summary>
/// <param name="path">文件路径(绝对路径)</param>
/// <returns>文件中的JSON字符串</returns>
public static string LoadJson(string path)
{
if (System.IO.File.Exists(path))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(path))
{
string json = sr.ReadToEnd();
sr.Close();
return json;
}
}
else
{
Debug.LogError("加载本地JSON文件:" + path + "——失败,未找到该文件");
return null;
}
}
/// <summary>
/// 清除JSON字符串中的转义字符
/// </summary>
/// <param name="json">JSON字符串</param>
/// <returns></returns>
public static string Clean(string json)
{
return JsonConvert.DeserializeObject(json).ToString();
}
/// <summary>
/// 反序列化一段JSON字符串
/// </summary>
/// <typeparam name="T">可以被反序列化的类型</typeparam>
/// <param name="json">JSON字符串</param>
/// <returns>反序列化后的对象</returns>
public static T FromJson<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// 序列化JSON对象
/// </summary>
/// <param name="obj">JSON对象</param>
/// <returns>序列化字符串</returns>
public static string ToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// 序列化JSON对象
/// </summary>
/// <param name="obj">JSON对象</param>
/// <param name="formatting">序列化格式</param>
/// <returns>序列化后的JSON字符串</returns>
public static string ToJson(object obj, Formatting formatting)
{
return JsonConvert.SerializeObject(obj, formatting);
}
/// <summary>
/// 反序列化本地JSON文件到相应的类
/// </summary>
/// <typeparam name="T">可以被反序列化的类型</typeparam>
/// <param name="path">JSON文件的路径</param>
/// <returns>反序列化后的对象</returns>
public static T FromFile<T>(string path)
{
string json = LoadJson(path);
if (string.IsNullOrEmpty(json))
{
return default(T);
}
var t = new JsonSerializerSettings();
t.NullValueHandling = NullValueHandling.Include;
return JsonConvert.DeserializeObject<T>(json, t);
}
/// <summary>
/// 将JSON对象保存到JSON文件
/// </summary>
/// <param name="obj">JSON对象</param>
/// <param name="path">文件路径(绝对路径)</param>
/// <returns>true 保存成功 false 保存失败</returns>
public static bool ToFile(object obj, string path)
{
if (string.IsNullOrEmpty(path)) return false;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path))
{
sw.Write(ToJson(obj));
sw.Close();
return true;
}
}
/// <summary>
/// 将JSON对象保存到JSON文件
/// </summary>
/// <param name="obj">JSON对象</param>
/// <param name="path">文件路径(绝对路径)</param>
/// <param name="append">true 添加内容 false 覆盖</param>
/// <returns>true 保存成功 false 保存失败</returns>
public static bool ToFile(object obj, string path, bool append)
{
if (string.IsNullOrEmpty(path)) return false;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, append))
{
sw.Write(ToJson(obj));
sw.Close();
return true;
}
}
/// <summary>
/// 获取某路径下JSON中的数组数据
/// </summary>
/// <typeparam name="T">类型(可序列化)</typeparam>
/// <param name="path">路径</param>
/// <returns></returns>
public static List<T> GetListDataFromPath<T>(string path)
{
if (!System.IO.File.Exists(path))
{
Debug.Log("文件不存在 path:" + path);
return null;
}
return FromFile<List<T>>(path);
}
/// <summary>
/// 获取Json字符串中的对象,直接存成list
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="json">Json字符串</param>
/// <returns></returns>
public static List<T> GetListDataFromString<T>(string json)
{
return JsonConvert.DeserializeObject<List<T>>(json);
}
/// <summary>
/// 转化List对象成字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static string GetStringFromListData<T>(List<T> list)
{
return JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.Indented);
}
/// <summary>
/// 将一组对象存储为Json字符串
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="path">存储路径</param>
/// <param name="list"></param>
public static void WriteListData<T>(string path, List<T> list)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, false))
{
string _json = JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.Indented);
sw.Write(_json);
sw.Close();
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="dic">字典</param>
/// <param name="key">字典里具体键</param>
/// <returns></returns>
public static int GetIntValue(Dictionary<string, object> dic, string key)
{
object value = GetValue(dic, key);
if (value == null)
{
return 0;
}
int intValue;
if (int.TryParse(value.ToString(), out intValue) == false)
{
Debug.Log(int.Parse(value.ToString()));
Debug.LogError("Value 转换失败 " + intValue);
}
return intValue;
}
public static string GetStringValue(Dictionary<string, object> from, string property)
{
object value = GetValue(from, property);
if (value == null)
{
return "";
}
return value.ToString();
}
public static object GetValue(Dictionary<string, object> from, string property)
{
if (from == null)
{
Debug.LogError("Dictionary<string, object> from = " + null);
return null;
}
object value;
if (from.TryGetValue(property, out value))
{
return value;
}
Debug.LogError("不存在property = " + property);
return null;
}
}
}