JsonUtils.cs 880 B

12345678910111213141516171819202122232425262728
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace BasePaySdk
  7. {
  8. class JsonUtils
  9. {
  10. public static string sort4JsonString(string sourceJson)
  11. {
  12. var dic = JsonConvert.DeserializeObject<SortedDictionary<string, object>>(sourceJson);
  13. SortedDictionary<string, object> keyValues = new SortedDictionary<string, object>(dic);
  14. var result = keyValues.OrderBy(m => m.Key);//升序 把Key换成Value 就是对Value进行排序
  15. //var result = keyValues.OrderByDescending(m => m.Key);//降序
  16. //简化为如下代码
  17. Dictionary<string, object> resultDic = result.ToDictionary(x => x.Key, x => x.Value);
  18. return JsonConvert.SerializeObject(resultDic);
  19. }
  20. }
  21. }