正在对 Json 数据进行排序

Sorting Json data

我讨厌对从 webresponse 收到的数据进行排序时遇到麻烦。

现在他们进来了 "random",而我根本无法对它进行排序。

现在部分 class 看起来像这样:

namespace computers{
using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
using J = Newtonsoft.Json.JsonPropertyAttribute;
public partial class GettingStarted
{
    [J("devices")] public Device[] Devices { get; set; }
}

public partial class Device
{
    [J("device_id")] public string DeviceId { get; set; }
    [J("assigned_to")] public bool AssignedTo { get; set; }
    [J("alias")] public string Alias { get; set; }
    [J("description")] public string Description { get; set; }
    [J("last_seen")] public string LastSeen { get; set; }
    [J("remotecontrol_id")] public string RemotecontrolId { get; set; }
    [J("groupid")] public string Groupid { get; set; }
    [J("online_state")] public string OnlineState { get; set; }
    [J("supported_features")] public string SupportedFeatures { get; set; }
}

public partial class GettingStarted
{
    public static GettingStarted FromJson(string json) => JsonConvert.DeserializeObject<GettingStarted>(json, Converter.Settings);
}

public static class Serialize
{
    public static string ToJson(this GettingStarted self) => JsonConvert.SerializeObject(self, Converter.Settings);
}

public class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
    };
}

如前所述,数据是随机出现的,我对它们进行排序的所有尝试都导致崩溃或什么都不做。

提前致谢!

您可以使用 lambda 表达式来实现此目的

添加using System.Linq指令

填充 'Devices' 数组后,使用它对其进行排序:

Devices = Devices.OrderBy(x=>x.Alias).ToArray()