using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; namespace MetaCity.BundleKit.Editor.Utils { /// /// JSON对象工具类 /// 用于序列化和反序列化JSON字符串 /// public static class JsonTools { /// /// 加载JSON文件 /// /// 文件路径(绝对路径) /// 文件中的JSON字符串 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; } } /// /// 清除JSON字符串中的转义字符 /// /// JSON字符串 /// public static string Clean(string json) { return JsonConvert.DeserializeObject(json).ToString(); } /// /// 反序列化一段JSON字符串 /// /// 可以被反序列化的类型 /// JSON字符串 /// 反序列化后的对象 public static T FromJson(string json) { return JsonConvert.DeserializeObject(json); } /// /// 序列化JSON对象 /// /// JSON对象 /// 序列化字符串 public static string ToJson(object obj) { return JsonConvert.SerializeObject(obj); } /// /// 序列化JSON对象 /// /// JSON对象 /// 序列化格式 /// 序列化后的JSON字符串 public static string ToJson(object obj, Formatting formatting) { return JsonConvert.SerializeObject(obj, formatting); } /// /// 反序列化本地JSON文件到相应的类 /// /// 可以被反序列化的类型 /// JSON文件的路径 /// 反序列化后的对象 public static T FromFile(string path) { string json = LoadJson(path); if (string.IsNullOrEmpty(json)) { return default(T); } var t = new JsonSerializerSettings(); t.NullValueHandling = NullValueHandling.Include; return JsonConvert.DeserializeObject(json, t); } /// /// 将JSON对象保存到JSON文件 /// /// JSON对象 /// 文件路径(绝对路径) /// true 保存成功 false 保存失败 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; } } /// /// 将JSON对象保存到JSON文件 /// /// JSON对象 /// 文件路径(绝对路径) /// true 添加内容 false 覆盖 /// true 保存成功 false 保存失败 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; } } /// /// 获取某路径下JSON中的数组数据 /// /// 类型(可序列化) /// 路径 /// public static List GetListDataFromPath(string path) { if (!System.IO.File.Exists(path)) { Debug.Log("文件不存在 path:" + path); return null; } return FromFile>(path); } /// /// 获取Json字符串中的对象,直接存成list /// /// 类型 /// Json字符串 /// public static List GetListDataFromString(string json) { return JsonConvert.DeserializeObject>(json); } /// /// 转化List对象成字符串 /// /// /// /// public static string GetStringFromListData(List list) { return JsonConvert.SerializeObject(list, Newtonsoft.Json.Formatting.Indented); } /// /// 将一组对象存储为Json字符串 /// /// 类型 /// 存储路径 /// public static void WriteListData(string path, List 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(); } } /// /// 反序列化 /// /// 字典 /// 字典里具体键 /// public static int GetIntValue(Dictionary 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 from, string property) { object value = GetValue(from, property); if (value == null) { return ""; } return value.ToString(); } public static object GetValue(Dictionary from, string property) { if (from == null) { Debug.LogError("Dictionary from = " + null); return null; } object value; if (from.TryGetValue(property, out value)) { return value; } Debug.LogError("不存在property = " + property); return null; } } }