C # Trier les clés de chaîne JSON

Je voudrais convertir la chaîne JSON

"{ \"birthday\": \"1988-03-18\", \"address\": { \"state\": 24, \"city\": 8341, \"country\": 1 } }" 

à

 "{ \"address\": { \"city\": 8341, \"country\": 1, \"state\": 24 }, \"birthday\": \"1988-03-18\" }" 

REMARQUE: je n’utilise pas la version sortingée pour la communication (car l’ordre des clés n’a pas d’importance). J’ai besoin d’une version sortingée pour effectuer des tests locaux (en comparant les chaînes JSON).


EDIT: I4V a souligné une solution qui utilise Json.Net , je préférerais utiliser une solution qui n’a pas besoin d’inclure une bibliothèque tierce (en fait, j’utilise le système intégré.Json dans mon application)


J’ai posté un résumé avec la solution fournie par I4V + quelques tests ici . Merci à tous.

Je vais utiliser Json.Net pour cela

 ssortingng json = @"{ ""birthday"": ""1988-03-18"", ""address"": { ""state"": 24, ""city"": 8341, ""country"": 1 } }"; var jObj = (JObject)JsonConvert.DeserializeObject(json); Sort(jObj); ssortingng newJson = jObj.ToSsortingng(); 

 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); } } 

MODIFIER

A essayer avec System.Json mais je ne suis pas sûr de OrderByDescending (ou OrderBy ).

 var jObj = (System.Json.JsonObject)System.Json.JsonObject.Parse(json); Sort2(jObj); var newJson = jObj.ToSsortingng(); 

 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); } } 

En utilisant cette approche, vous pouvez récupérer un object dynamic avec vos données json

Au DynamicJsonConverter créez un SortedDictionary

 var d = new SortedDictionary(dictionary); // TODO: code to sort inner objects return new DynamicJsonObject(d); 

Ensuite, vous pouvez utiliser

 ssortingng jsonStr = "{\"B\":\"2\",\"A\":\"1\"}"; JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic json = jss.Deserialize(jsonStr, typeof(object)) as dynamic; ssortingng result = new JavaScriptSerializer().Serialize((json as DynamicJsonObject).Dictionary); 

Et le result aura la sortie attendue.

Je sais que cela peut être un peu tard, mais dans le cas où vous auriez besoin de sortinger les tableaux internes de données aussi (j’en avais juste besoin):

 static 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); if (prop.Value is JArray) { Int32 iCount = prop.Value.Count(); for (Int32 iIterator = 0; iIterator < iCount; iIterator++) if (prop.Value[iIterator] is JObject) Sort((JObject)prop.Value[iIterator]); } } } 

À votre santé!