1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace KdanCommon.Helpers
- {
- public static class DynamicJsonHelper
- {
- public static dynamic ToDynamicJson(string jsonString)
- {
- try
- {
- JObject jo = JObject.Parse(jsonString);
- return jo as dynamic;
- }
- catch
- {
- return null;
- }
- }
- public static T GetValueSafety<T>(dynamic value, T defaultValue)
- {
- try
- {
- var val = (T)value;
- if (val == null)
- return defaultValue;
- else
- return (T)value;
- }
- catch
- {
- return defaultValue;
- }
- }
- public static List<dynamic> GetArraySafety(dynamic value)
- {
- try
- {
- if (value is JArray)
- return ((JArray)value).Cast<dynamic>().ToList();
- }
- catch { }
- return null;
- }
- }
- }
|