| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.Xml;
- using Assets.Plugins.Log;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
- namespace Assets.Plugins.CommonTool
- {
- public static class JsonObjectHelper
- {
- #region Json
- public static string Obj2String(this object source)
- {
- try
- {
- return JsonConvert.SerializeObject(source);
- }
- catch (Exception e)
- {
- typeof(JsonObjectHelper).LogError(e);
- return "";
- }
- }
- public static string Obj2StringCamelCase(this object source)
- {
- return Obj2String(source, true);
- }
- public static string Obj2String(this object source,bool isCamelCase)
- {
- try
- {
- var setting = new JsonSerializerSettings
- {
- ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
- };
- return JsonConvert.SerializeObject(source,setting);
- }
- catch (Exception e)
- {
- typeof(JsonObjectHelper).LogError(e);
- return "";
- }
- }
- public static T Str2Obj<T>(this string source)
- {
- return GetModel<T>(source);
- }
- public static T GetModel<T>(this string source)
- {
- try
- {
- if (string.IsNullOrEmpty(source))
- {
- return default(T);
- }
- return JsonConvert.DeserializeObject<T>(source);
- }
- catch (Exception e)
- {
- typeof(JsonObjectHelper).LogError(e);
- return default(T);
- }
- }
- //public static T GetObj<T>(this string source, string key = null)
- //{
- // try
- // {
- // var converter = new ExpandoObjectConverter();
- // var jsonObject = JsonConvert.DeserializeObject<ExpandoObject>(source, converter);
- // if (string.IsNullOrEmpty(key))
- // {
- // T info = JsonConvert.DeserializeObject<T>(source);
- // return info;
- // }
- // object obj = jsonObject.GetValue(key);
- // return (T)obj;
- // }
- // catch (Exception e)
- // {
- // typeof(JsonObjectHelper).LogError(e);
- // return default(T);
- // }
- //}
- //public static T GetResultModel<T>(this string objStr, bool isLower = true)
- //{
- // try
- // {
- // var converter = new ExpandoObjectConverter();
- // dynamic jsonObject = JsonConvert.DeserializeObject<ExpandoObject>(objStr, converter);
- // if (isLower)
- // {
- // if (jsonObject.success)
- // {
- // T info = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jsonObject.result));
- // return info;
- // }
- // }
- // else
- // {
- // if (jsonObject.Success)
- // {
- // T info = JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(jsonObject.Result));
- // return info;
- // }
- // }
- // }
- // catch (Exception e)
- // {
- // typeof(JsonObjectHelper).LogError(e);
- // }
- // return default(T);
- //}
- #endregion Json
- #region XML
-
- /// <summary>
- /// 返回指定节点下信息的JSON格式字符串
- /// </summary>
- /// <param name="str">xml字符串</param>
- /// <param name="nodeName">节点名称,应从根节点开始</param>
- /// <returns></returns>
- public static string Xml2Json(string str, string nodeName)
- {
- XmlDocument xDoc = new XmlDocument();
- xDoc.LoadXml(str);
- XmlNode node = xDoc.SelectSingleNode(nodeName);
- var result = JsonConvert.SerializeXmlNode(node);
- return result;
- }
-
- /// <summary>
- /// 返回指定节点下信息的JSON格式字符串
- /// </summary>
- /// <param name="node">xml</param>
- /// <param name="nodeName">节点名称,应从根节点开始</param>
- /// <returns></returns>
- public static string Xml2Json(this XmlNode node, string nodeName=null)
- {
- if (nodeName != null && nodeName.IsNotEmpty())
- {
- node = node.SelectSingleNode(nodeName);
- }
- string result = JsonConvert.SerializeXmlNode(node);
- return result;
- }
- /// <summary>
- /// json转成xml
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string Json2Xml(string str)
- {
- XmlDocument xml = JsonConvert.DeserializeXmlNode(str);
- string result = xml.OuterXml;
- return result;
- }
- #endregion
- ///// <summary>
- ///// Gets the value.
- ///// </summary>
- ///// <param name="obj">The object.</param>
- ///// <param name="key">The key.</param>
- ///// <returns>System.Object.</returns>
- //public static dynamic GetValue(this object obj, string key)
- //{
- // if (obj is List<object> oos)
- // {
- // foreach (var oo in oos)
- // {
- // var result = GetValue(oo, key);//递归返回匹配的值
- // if (result != null)
- // return result;
- // }
- // }
- // else if (obj is object[] objects)
- // {
- // foreach (var oo in objects)
- // {
- // var result = GetValue(oo, key);//递归返回匹配的值
- // if (result != null)
- // return result;
- // }
- // }
- // else if (obj is IDictionary<string, object> dictionarys)
- // {
- // foreach (var dic in dictionarys)
- // {
- // if (dic.Key == key || dic.Key == key.ToLower() || dic.Key == key.ToUpper())
- // return dic.Value;
- // }
- // //如果上面的遍历没有结果,则该值可能嵌套在property.Value里面,需要递归解析
- // foreach (var dic in dictionarys)
- // {
- // var result = GetValue(dic.Value, key);//递归返回匹配的值
- // if (result != null)
- // return result;
- // }
- // }
- // return null;//没有匹配值,返回null
- //}
- }
- }
|