using Alchemy.Core.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Alchemy.Core.Services
{
public class JsonService
{
///
/// 尝试获取
///
///
///
///
public static string TryGetValue(string strJson, string strKey)
{
JObject? jo = strJson.FromJson();
string strResullt = string.Empty;
if (jo.ContainsKey(strKey))
strResullt = GetValue(strJson, strKey);
return strResullt;
}
///
/// 写入JSON
///
///
///
public static void WriteJson(FileInfo fi, string strValue)
{
WriteJson(fi.FullName, strValue);
}
///
/// 写入JSON
///
///
///
public static void WriteJson(string strJsonPath, string strJson)
{
JObject? jo = strJson.FromJson();
string output = JsonConvert.SerializeObject(jo, Formatting.Indented);
File.WriteAllText(strJsonPath, output);
}
///
/// 写入JSON
///
///
///
public static void WriteJsonArray(FileInfo fi, string strJson)
{
WriteJsonArray(fi.FullName, strJson);
}
///
/// 写入JSON
///
///
///
public static void WriteJsonArray(string strJsonPath, string strJson)
{
JArray? ja = strJson.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 = strJson.FromJson();
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 = strJson.FromJson();
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 = strJson.FromJson();
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 = strJson.FromJson();
return ja[index][strKey].ToJson();
}
internal static string GetValue(object filePath_AppSettingsJson, string strKey)
{
throw new NotImplementedException();
}
}
}