#region json字符串字典序排序
public static string SortJson(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);//降序
SortedDictionary<string, object> tempKeyValues = new SortedDictionary<string, object>(keyValues);
foreach (KeyValuePair<string, object> kv in tempKeyValues)
{
// 判断value是不是JObject类型
Type t0 = typeof(JObject);
Type t1 = kv.Value.GetType();
if (t0 == t1) {
// value是JObject类型
string jsonItem = JsonConvert.SerializeObject(kv.Value);
jsonItem = SortJson(jsonItem);
keyValues[kv.Key] = JsonConvert.DeserializeObject<JObject>(jsonItem);
}
}
return JsonConvert.SerializeObject(keyValues);
}
#endregion
C# JSON字符串字典序排序(递归)
最新推荐文章于 2023-08-21 09:37:03 发布