using Alchemy.Core.Extensions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Alchemy.Core.Services { public class JsonService { /// /// 写入JSON /// /// /// public static void WriteJson(FileInfo fi, string strValue) { WriteJson(fi.FullName, strValue); } /// /// 写入JSON /// /// /// public static void WriteJson(string strJsonPath, string strValue) { JObject jo = strValue.FromJson(); string output = JsonConvert.SerializeObject(jo, Formatting.Indented); File.WriteAllText(strJsonPath, output); } /// /// 写入JSON /// /// /// public static void WriteJsonArray(FileInfo fi, string strValue) { WriteJsonArray(fi.FullName, strValue); } /// /// 写入JSON /// /// /// public static void WriteJsonArray(string strJsonPath, string strValue) { JArray ja = strValue.FromJson(); string output = JsonConvert.SerializeObject(ja, Formatting.Indented); File.WriteAllText(strJsonPath, output); } /// /// 读取文件路径,写入Json内容 /// /// /// /// public static void SetJson(FileInfo fi, string strKey, string strValue) { string strJson = File.ReadAllText(fi.FullName); SetJson(fi.FullName, strJson, strKey, strValue); } /// /// 读取文件路径,写入Json内容 /// /// /// /// /// public static void SetJson(string strJsonPath, string strJson, string strKey, string strValue) { JObject jo = strJson.FromJson(); jo[strKey] = strValue.FromJson(); string output = JsonConvert.SerializeObject(jo, Formatting.Indented); File.WriteAllText(strJsonPath, output); } /// /// 读取文件路径,写入Json内容 /// /// /// /// public static void SetValue(FileInfo fi, string strKey, string strValue) { string strJson = FileService.ReadAll(fi.FullName); SetValue(fi.FullName, strJson, strKey, strValue); } /// /// 读取文件路径,写入Json内容 /// /// /// /// /// public static void SetValue(string strJsonPath, string strJson, string strKey, string strValue) { JObject jo = (JObject)JsonConvert.DeserializeObject(strJson); jo[strKey] = strValue; string output = JsonConvert.SerializeObject(jo, Formatting.Indented); FileService.WriteAll(strJsonPath, output); } /// /// 读取文件路径,获取Json内容 /// /// /// public static string GetAllContent(string strFilePath) { return FileService.ReadAll(strFilePath); } /// /// 读取文件路径,获取Json内容 /// /// /// public static string GetAllContent(FileInfo fi) { return GetAllContent(fi.FullName); } /// /// 读取文件路径,从Json字符串的对象中获取Json字符串 /// /// /// /// public static string GetJson(FileInfo fi, string strKey) { string strJson = FileService.ReadAll(fi.FullName); return GetJson(strJson, strKey); } /// /// 读取文件内容,从Json字符串的对象中获取Json字符串 /// /// /// /// public static string GetJson(string strJson, string strKey) { JObject jo = (JObject)JsonConvert.DeserializeObject(strJson); return jo[strKey].ToJson(); } /// /// 读取文件路径,从Json字符串的对象中获取值 /// /// /// /// public static string GetValue(FileInfo fi, string strKey) { string strJson = FileService.ReadAll(fi.FullName); return GetValue(strJson, strKey); } /// /// 读取文件内容,从Json字符串的对象中获取值 /// /// /// /// public static string GetValue(string strJson, string strKey) { JObject jo = (JObject)JsonConvert.DeserializeObject(strJson); return jo[strKey].ToString(); } /// /// 读取文件路径,从Json字符串的数组中获取值 /// /// /// /// /// public static string GetValue(FileInfo fi, int index, string strKey) { string strJson = FileService.ReadAll(fi.FullName); return GetValue(strJson, index, strKey); } /// /// 读取文件内容,从Json字符串的数组中获取值 /// /// /// /// /// public static string GetValue(string strJson, int index, string strKey) { JArray ja = (JArray)JsonConvert.DeserializeObject(strJson); return ja[index][strKey]?.ToJson(); } } }