.net 對JSON資料排序

【驀然回首】發表於2018-03-06
string json = @"
[
    {
        ""col1"": ""foo"",
        ""col2"": ""bar""
    },
    {
        ""col1"": ""baz"",
        ""col2"": ""quux""
    },
    {
        ""col1"": ""fizz"",
        ""col2"": ""bang""
    }
]";

JArray array = JArray.Parse(json);

JArray sorted = new JArray(array.OrderBy(obj => (string)obj["col2"]));//升序
json0.OrderByDescending(x => (string)x["order"]);//按order進行降序排序
Console.WriteLine(sorted.ToString(Formatting.Indented));

相關文章