我将为此使用Json.Net
string json = @"{ ""birthday"": ""1988-03-18"", ""address"": { ""state"": 24, ""city"": 8341, ""country"": 1 } }";var jObj = (JObject)JsonConvert.DeserializeObject(json);Sort(jObj);string newJson = jObj.ToString();
void Sort(JObject jObj){ var props = jObj.Properties().ToList(); foreach (var prop in props) { prop.Remove(); } foreach (var prop in props.OrderBy(p=>p.Name)) { jObj.Add(prop); if(prop.Value is JObject) Sort((JObject)prop.Value); }}
编辑
尝试一下,
System.Json但我不确定
OrderByDescending(或
OrderBy)。
var jObj = (System.Json.JsonObject)System.Json.JsonObject.Parse(json);Sort2(jObj);var newJson = jObj.ToString();
void Sort2(System.Json.JsonObject jObj){ var props = jObj.ToList(); foreach (var prop in props) { jObj.Remove(prop.Key); } foreach (var prop in props.OrderByDescending(p => p.Key)) { jObj.Add(prop); if (prop.Value is System.Json.JsonObject) Sort2((System.Json.JsonObject)prop.Value); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)