C# 如何对json格式的字符串进行字典排序?
字典排序前:{"aa":"aa1023","cc":"c0003","bb":"b003","data":[{}],"acf":"v0001"}字典排序后:{"aa":"...
字典排序前:
{"aa":"aa1023","cc":"c0003","bb":"b003","data":[{}],"acf":"v0001"}
字典排序后:
{"aa":"aa1023","acf":"v0001","bb":"b003","cc":"c0003","data":[{}]} 展开
{"aa":"aa1023","cc":"c0003","bb":"b003","data":[{}],"acf":"v0001"}
字典排序后:
{"aa":"aa1023","acf":"v0001","bb":"b003","cc":"c0003","data":[{}]} 展开
2个回答
展开全部
如果拿到json字段串,直接转成字典呀,再排序,再字典转json就行了。
项目要引用 System.Web.Extensions.dll
命名空间
using System.Web.Extensions;
{
string str = "{\"aa\":\"aa1023\",\"cc\":\"c0003\",\"bb\":\"b003\",\"data\":[{}],\"acf\":\"v0001\"}";
JavaScriptSerializer jss = new JavaScriptSerializer();
//根据Key来排序,并生成新的字典
var lst = jss.Deserialize<Dictionary<string, object>>(str).OrderBy(a => a.Key).ToDictionary(o=>o.Key, p=>p.Value);
var ssss = jss.Serialize(lst); //反json
Console.WriteLine(ssss);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询