Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Found this solution:
public static void Main()
{
string json = @"{300:[{'Lygis':2,'Patiekalo_ID':30}],110:[{'Lygis':3,'Patiekalo_ID':31}]}";
var jObj = (JObject)JsonConvert.DeserializeObject(json);
Sort(jObj);
string newJson = jObj.ToString();
Console.WriteLine(newJson);
}
static void Sort(JObject jObj)
{
var props = jObj.Properties().ToList();
foreach (var prop in props)
{
prop.Remove();
}
foreach (var prop in props.OrderBy(p=>Int32.Parse(p.Name)))
{
jObj.Add(prop);
if(prop.Value is JObject)
Sort((JObject)prop.Value);
}
}
1. Parse all json in Dictionary using Newtonsoft.Json or other means;
2. Sort through Linq either by dictionary keys or by values.
Json is text, it is not sortable.
Json needs to be converted to an object (data model). But even there you can only sort lists, but you don’t have lists.
See all sorts of SimpleJson, Newtonsoft and others.
var json = "{\r\n \"30\": {\r\n \"3\":{},\r\n \"0\":{}\r\n },\r\n \"11\":{}\r\n}";
var dict = JsonConvert.DeserializeObject<Dictionary<int, Dictionary<int, object>>>(json);
var res = dict.OrderBy(s => s.Key).ToDictionary(s=> s.Key, s=> s.Value);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question