using Furion.FriendlyException;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
namespace YBEE.EQM.Core
{
///
/// Json工具类
///
public static class JsonUtil
{
///
/// Object转Josn
///
///
///
public static string ToJson(this object obj)
{
if (obj == null)
{
return null;
}
return JsonConvert.SerializeObject(obj);
}
///
/// Json转Object
///
///
///
///
public static T FromJson(this string str)
{
try
{
return JsonConvert.DeserializeObject(str);
}
catch (Exception ex)
{
throw Oops.Oh(ex.Message);
// return default(T);
}
}
///
/// 获取Json的Model
///
///
///
///
public static T ParseFromJson(string szJson)
{
if (typeof(T) == typeof(IEnumerable<>))
{
}
T obj = Activator.CreateInstance();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(ms);
}
}
///
/// string字典
///
///
///
///
///
public static Dictionary DeserializeStringToDictionary(string jsonStr)
{
if (string.IsNullOrEmpty(jsonStr))
return new Dictionary();
Dictionary jsonDict = JsonConvert.DeserializeObject>(jsonStr);
return jsonDict;
}
}
}