我在尝试将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 :(得分:0)
我最终做的是重复使用我的旧代码。所以现在我有了这个:
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的人重复使用。这对选择选项有效:
colModel: {name: 'status_id', label: 'Status', edittype: 'select', sortable: true, search: true, stype:'select', editoptions: " + statuses + @", searchoptions: {sopt: ['eq', 'ne']}}