【问题标题】:Enum to JSON object for jQGrid using JSON.NET使用 JSON.NET 枚举到 jQGrid 的 JSON 对象
【发布时间】:2012-02-05 01:24:28
【问题描述】:

我在尝试将 Enum 转换为 jQGrid 的 JSON 字符串时遇到问题。我之前使用的格式(进行手动转换)是这样的:

{{0: '-', 1: 'Active', 2: 'Deactive', 3: 'Pending'}}

    public static string GetStatuses(bool addDefault = false)
    {
        var statusesEnum = Enum.GetValues(typeof(StatusEnum));
        string statuses = "{value: {0: '-', ";

        foreach (StatusEnum status in statusesEnum)
            statuses += String.Format("{0}: '{1}', ", (byte)status, Enum.GetName(typeof(StatusEnum), status));

        return statuses.Substring(0, statuses.Length - 2) + "}}";
    }

所以我需要避免这种方法,因为我认为这不是最好的方法,我想使用 JSON.NET 库对其进行序列化。所以我写了这个:

public class StatusJSON
{
    public byte ID { get; set; }
    public string Name { get; set; }

    public StatusJSON() { }

    public StatusJSON(byte id, string name)
    {
        ID = id;
        Name = name;
    }
}

public class JSONUtils
{
    /// <summary>
    /// Get all the posible statuses of selected <paramref name="type"/> in JSON
    /// </summary>
    /// <param name="type">Type of the status</param>
    /// <param name="addDefault">Check if add a default / NULL status</param>
    /// <returns>A string JSON with the statuses</returns>
    public static string GetStatuses(Type type, bool addDefault = false)
    {
        var statusesEnum = Enum.GetValues(type);
        List<StatusJSON> statuses = new List<StatusJSON>();

        if (addDefault)
            statuses.Add(new StatusJSON(0, "-"));

        foreach (var statusEnum in statusesEnum)
            statuses.Add(new StatusJSON((byte)statusEnum, Enum.GetName(type, statusEnum)));

        return JsonConvert.SerializeObject(statuses);
    }
}

您可以将其用作:string statuses = JSONUtils.GetStatuses(typeof(StatusEnum), addDefault);。问题是这样返回一个字符串:

[{"ID":0,"Name":"-"},{"ID":1,"Name":"Active"},{"ID":2,"Name":"Deactive"},{"ID":3,"Name":"Pending"}]

库中有什么方法可以获取我需要的字符串吗?谢谢

【问题讨论】:

  • 我不认为,因为 {{0: '-', 1: 'Active', 2: 'Deactive', 3: 'Pending'}} 不是有效的 json 字符串。也许你应该看看StringEnumConverter class

标签: c# asp.net json enums json.net


【解决方案1】:

我最终所做的是重新使用我的旧代码。所以现在我有了这个:

public class Statutes
{
    public byte ID { get; set; }
    public string Name { get; set; }

    public Statutes() { }

    public Statutes(byte id, string name)
    {
        ID = id;
        Name = name;
    }

    /// <summary>
    /// Get all the posible statuses of selected <paramref name="type"/>
    /// </summary>
    /// <param name="type">Type of the status</param>
    /// <param name="addDefault">Check if add a default / NULL status</param>
    /// <returns>A list with the statuses</returns>
    public static List<Statutes> SelectAll(Type type, bool addDefault = false)
    {
        var statusesEnum = Enum.GetValues(type);
        List<Statutes> statuses = new List<Statutes>();

        if (addDefault)
            statuses.Add(new Statutes(0, "-"));

        foreach (var statusEnum in statusesEnum)
            statuses.Add(new Statutes((byte)statusEnum, Enum.GetName(type, statusEnum)));

        return statuses;
    }
}

public class JSONUtils
{
    /// <summary>
    /// Get all the posible statuses of selected <paramref name="type"/> in JSON
    /// </summary>
    /// <param name="type">Type of the status</param>
    /// <param name="addDefault">Check if add a default / NULL status</param>
    /// <returns>A string JSON for jQGrid with the statuses</returns>
    public static string GetStatusesJQGrid(Type type, bool addDefault = false)
    {
        var statuses = Statutes.SelectAll(type, addDefault);
        string result = "{value: {";

        foreach (Statutes status in statuses)
            result += String.Format("{0}: '{1}', ", status.ID, status.Name);

        return result.Substring(0, result.Length - 2) + "}}";
    }
}

您可以将其用作:string statuses = JSONUtils.GetStatusesJQGrid(typeof(StatusEnum), true);

在我找到使用 JSON.NET 的更好方法之前,我认为这是一段很好的代码,可供使用 jQGrid 的人重复使用。这对 select 选项有效:

colModel: {name: 'status_id', label: 'Status', edittype: 'select', sortable: true, search: true, stype:'select', editoptions: " + statuses + @", searchoptions: {sopt: ['eq', 'ne']}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多