using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
namespace CommonBase
{
///
/// JObject扩展
///
public static class JObjectExtensions
{
///
/// 将JObject转化成字典
///
///
///
public static dict Todict(this JToken json)
{
var propertyValuePairs = json.ToObject();
ProcessJObjectProperties(propertyValuePairs);
ProcessJArrayProperties(propertyValuePairs);
return propertyValuePairs;
}
private static void ProcessJObjectProperties(dict propertyValuePairs)
{
var objectPropertyNames = (from property in propertyValuePairs
let propertyName = property.Key
let value = property.Value
where value is JObject
select propertyName).ToList();
objectPropertyNames.ForEach(propertyName => propertyValuePairs[propertyName] = Todict((JObject)propertyValuePairs[propertyName]));
}
private static void ProcessJArrayProperties(dict propertyValuePairs)
{
var arrayPropertyNames = (from property in propertyValuePairs
let propertyName = property.Key
let value = property.Value
where value is JArray
select propertyName).ToList();
//arrayPropertyNames.ForEach(propertyName => propertyValuePairs[propertyName] = ToArray((JArray)propertyValuePairs[propertyName]));
arrayPropertyNames.ForEach(propertyName => propertyValuePairs[propertyName] = ToList((JArray)propertyValuePairs[propertyName]));
}
///
///
///
///
///
public static object[] ToArray(this JArray array)
{
return array.ToObject().Select(ProcessArrayEntry).ToArray();
}
public static List