c# 对JSON字符串排序(KEY/VALUE)


public string StortJson(string json)
{
      var dic = JsonConvert.DeserializeObject<SortedDictionary<string, object>>(json);
      SortedDictionary<string, object> keyValues = new SortedDictionary<string, object>(dic);
      keyValues.OrderBy(m => m.Key);//升序 把Key换成Value 就是对Value进行排序
      //keyValues.OrderByDescending(m => m.Key);//降序
      return JsonConvert.SerializeObject(keyValues);
}

需要添加的引用:

using System.Collections.Generic;

using System.Linq;

using Newtonsoft.Json;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM