我有一个来自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#排序JSON字符串键
但我不知道如何从解决方案加载我的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);
}