Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中的有序Json属性列表#_C#_.net_Json_Json.net - Fatal编程技术网

C# C中的有序Json属性列表#

C# C中的有序Json属性列表#,c#,.net,json,json.net,C#,.net,Json,Json.net,我有一个如下所示的类,我使用JSON.NET来序列化/反序列化 public class Car { [JsonProperty(Order = 3)] public int CarId {get; set;} [JsonProperty(Order = 1)] public string CarName {get; set;} [JsonIgnore] public int CarMfgDate {get; set} [JsonProperty(Order = 2)

我有一个如下所示的类,我使用JSON.NET来序列化/反序列化

public class Car
{
  [JsonProperty(Order = 3)]
  public int CarId {get; set;}
  [JsonProperty(Order = 1)]
  public string CarName {get; set;}
  [JsonIgnore]
  public int CarMfgDate {get; set}
  [JsonProperty(Order = 2)]
  public int CarCapacity {get; set;}
}
我需要获得所有Json可见属性的列表,其顺序与上面类中“JsonProperty”属性定义的顺序相同。因此,我希望对“Car”类的列表进行如下初始化

List<string> columns = GetOrderedJSONProperties();

Console.WriteLine(columns.Count); //Prints "3"
Console.WriteLine(columns[0]); //Prints "CarName"
Console.WriteLine(columns[1]); //Prints "CarCapacity"
Console.WriteLine(columns[2]); //Prints "CarId"
List columns=GetOrderedJSONProperties();
Console.WriteLine(columns.Count)//打印“3”
Console.WriteLine(列[0])//印刷“卡纳姆”
Console.WriteLine(列[1])//打印“CarCapacity”
Console.WriteLine(第[2]列)//打印“CarId”

请帮助我编写GetOrderedJSONProperties()方法。谢谢你的帮助

您可以使用Linqs
OrderBy
函数:

var columns = typeof(Car).GetProperties()
    .Where(p => p.GetCustomAttribute<JsonIgnoreAttribute>() == null)
    .OrderBy(p => p.GetCustomAttribute<JsonPropertyAttribute>()?.Order)
    .Select(p => p.Name).ToList();
var columns=typeof(Car).GetProperties()
.Where(p=>p.GetCustomAttribute()==null)
.OrderBy(p=>p.GetCustomAttribute()?.Order)
.Select(p=>p.Name).ToList();

您可以使用Linqs
OrderBy
功能:

var columns = typeof(Car).GetProperties()
    .Where(p => p.GetCustomAttribute<JsonIgnoreAttribute>() == null)
    .OrderBy(p => p.GetCustomAttribute<JsonPropertyAttribute>()?.Order)
    .Select(p => p.Name).ToList();
var columns=typeof(Car).GetProperties()
.Where(p=>p.GetCustomAttribute()==null)
.OrderBy(p=>p.GetCustomAttribute()?.Order)
.Select(p=>p.Name).ToList();

这项工作进行了一些更改。我已将更改后的代码粘贴到下面
var columns=typeof(Car).GetProperties().Where(p=>p.GetCustomAttributes(typeof(JsonIgnoreAttribute),false)==null | | p.GetCustomAttributes(typeof(JsonIgnoreAttribute),false)。Length==0.OrderBy(p=>((JsonPropertyAttribute[])p.GetCustomAttributes(typeof(JsonPropertyAttribute),false))[0].Order)。选择(p=>p.Name.ToList()。谢谢你给我指明了正确的方向。我接受这个答案,但有上面的警告。@lefou带有类型的属性getter作为扩展方法存在:感谢链接@Nico!这项工作进行了一些修改。我已将更改后的代码粘贴到下面
var columns=typeof(Car).GetProperties().Where(p=>p.GetCustomAttributes(typeof(JsonIgnoreAttribute),false)==null | | p.GetCustomAttributes(typeof(JsonIgnoreAttribute),false)。Length==0.OrderBy(p=>((JsonPropertyAttribute[])p.GetCustomAttributes(typeof(JsonPropertyAttribute),false))[0].Order)。选择(p=>p.Name.ToList()。谢谢你给我指明了正确的方向。我接受这个答案,但有上面的警告。@lefou带有类型的属性getter作为扩展方法存在:感谢链接@Nico!