chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
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
using System.Collections.Generic;
using Tea;
using Newtonsoft.Json;
 
namespace Alipay.EasySDK.Kernel.Util
{
    /// <summary>
    /// JSON工具类
    /// </summary>
    public class JsonUtil
    {
        /// <summary>
        /// 将字典集合转换为Json字符串,转换过程中对于TeaModel,使用标注的字段名称而不是字段的变量名
        /// </summary>
        /// <param name="input">字典集合</param>
        /// <returns>Json字符串</returns>
        public static string ToJsonString(IDictionary<string, object> input)
        {
            IDictionary<string, object> result = new Dictionary<string, object>();
            foreach (var pair in input)
            {
                if (pair.Value is TeaModel)
                {
                    result.Add(pair.Key, GetTeaModelMap((TeaModel)pair.Value));
                }
                else
                {
                    result.Add(pair.Key, pair.Value);
                }
            }
            return JsonConvert.SerializeObject(result);
        }
 
        private static IDictionary<string, object> GetTeaModelMap(TeaModel teaModel)
        {
 
            IDictionary<string, object> result = new Dictionary<string, object>();
            IDictionary<string, object> teaModelMap = teaModel.ToMap();
            foreach (var pair in teaModelMap)
            {
                if (pair.Value is TeaModel)
                {
                    result.Add(pair.Key, GetTeaModelMap((TeaModel)pair.Value));
                }
                else
                {
                    result.Add(pair.Key, pair.Value);
                }
            }
            return result;
        }
    }
}