Reputation: 343
I have a JSON file from our CRM:
[{"name": "erwin", "type":"ma", "id":"129"}, {"name": "hans", "type":"tf", "id":"12"}]
Now I need to sort this JSON by ID value, in my example the output should be:
[{"name": "hans", "type":"tf", "id":"12"}, {"name": "erwin", "type":"ma", "id":"129"}]
I already found this thread: C# Sort JSON string keys
But I don`t know how to load my JSON file into the method from the solution. Maybe with json.net? Regards,
Francis
EDIT:
string sourcePath = @Settings.Default.folder;
string pathToSourceFile = Path.Combine(sourcePath, "myfile.json");
var list = JsonConvert.DeserializeObject<List<Gesamtplan>>(File.ReadAllText(pathToSourceFile));
Upvotes: 4
Views: 20975
Reputation: 6203
Try like this
using Newtonsoft.Json;
public class RootObject
{
public string name { get; set; }
public string type { get; set; }
public string id { get; set; }
}
private string AscMyJson(string json)
{
var listOb = JsonConvert.DeserializeObject<List<RootObject>>(json);
var descListOb = listOb.OrderBy(x => x.id);
return JsonConvert.SerializeObject(descListOb);
}
Upvotes: 4