using System.Collections.Generic; using Tea; using Newtonsoft.Json; namespace Alipay.EasySDK.Kernel.Util { /// /// JSON工具类 /// public class JsonUtil { /// /// 将字典集合转换为Json字符串,转换过程中对于TeaModel,使用标注的字段名称而不是字段的变量名 /// /// 字典集合 /// Json字符串 public static string ToJsonString(IDictionary input) { IDictionary result = new Dictionary(); 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 GetTeaModelMap(TeaModel teaModel) { IDictionary result = new Dictionary(); IDictionary 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; } } }