我有一个来 self 们 CRM 的 JSON 文件:
[{"name": "erwin", "type":"ma", "id":"129"}, {"name": "hans", "type":"tf", "id":"12"}]
现在我需要按 ID 值对这个 JSON 进行排序,在我的示例中输出应该是:
[{"name": "hans", "type":"tf", "id":"12"}, {"name": "erwin", "type":"ma", "id":"129"}]
我已经找到这个线程:C# Sort JSON string keys
但我不知道如何将我的 JSON 文件加载到解决方案的方法中。 也许用 json.net? 问候,
弗朗西斯
编辑:
string sourcePath = @Settings.Default.folder;
string pathToSourceFile = Path.Combine(sourcePath, "myfile.json");
var list = JsonConvert.DeserializeObject<List<Gesamtplan>>(File.ReadAllText(pathToSourceFile));
最佳答案
这样试试
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);
}
关于C# 为值排序 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39408158/