cloudflight
2023-11-23 2284ddda0e49d402ff901d6691e937af4c628858
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
 
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
namespace CommonBase
{
    /// <summary>
    /// JObject扩展
    /// </summary>
    public static class JObjectExtensions
    {
        /// <summary>
        /// 将JObject转化成字典
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static dict Todict(this JToken json)
        {
            var propertyValuePairs = json.ToObject<dict>();
            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]));
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static object[] ToArray(this JArray array)
        {
            return array.ToObject<dynamic[]>().Select(ProcessArrayEntry).ToArray();
        }
        public static List<object> ToList(this JArray array)
        {
            return array.ToObject<dynamic[]>().Select(ProcessArrayEntry).ToList();
        }
 
        private static dynamic ProcessArrayEntry(dynamic value)
        {
            if (value is JObject)
            {
                return Todict((JObject)value);
            }
            if (value is JArray)
            {
                return ToList((JArray)value);
                //return ToArray((JArray)value);
            }
            return value;
        }
 
    }
}