C# 将Json键值对解析为POCO属性

C# 将Json键值对解析为POCO属性,c#,json,C#,Json,我收到的Json如下所示: [ { "FieldName": "Note", "Value": "Test of a note" }, { "FieldName": "P&IPayment", "Value": "Payment amount" }, { "

我收到的Json如下所示:

[
  {
    "FieldName": "Note",
    "Value": "Test of a note"
  }, 
  {
    "FieldName": "P&IPayment",
    "Value": "Payment amount"
  }, 
  {
    "FieldName": "000-002",
    "Value": "ABC"
  }, 
]
    public class ExportModel
    {
        [Newtonsoft.Json.JsonPropertyAttribute("Note")]
        public string Note { get; set; }

        [Newtonsoft.Json.JsonPropertyAttribute("P&IPAYMENT")]
        public string PAndIPayment { get; set; }

        [Newtonsoft.Json.JsonPropertyAttribute("000-002")]
        public string MajorLoanType { get; set; }
    }
我想将其反序列化为如下所示的类:

[
  {
    "FieldName": "Note",
    "Value": "Test of a note"
  }, 
  {
    "FieldName": "P&IPayment",
    "Value": "Payment amount"
  }, 
  {
    "FieldName": "000-002",
    "Value": "ABC"
  }, 
]
    public class ExportModel
    {
        [Newtonsoft.Json.JsonPropertyAttribute("Note")]
        public string Note { get; set; }

        [Newtonsoft.Json.JsonPropertyAttribute("P&IPAYMENT")]
        public string PAndIPayment { get; set; }

        [Newtonsoft.Json.JsonPropertyAttribute("000-002")]
        public string MajorLoanType { get; set; }
    }
我希望字段名映射到属性的属性,值映射到属性


感谢您的帮助。

您必须更改值的名称:

[Newtonsoft.Json.JsonPropertyAttribute("Note")]
public string Note { get; set; } CHANGE FOR  ---> 
[Newtonsoft.Json.JsonPropertyAttribute("FieldName")]
public string FieldName { get; set; }


[Newtonsoft.Json.JsonPropertyAttribute("P&IPAYMENT")]
public string P&IPAYMENT{ get; set; } CHANGEFOR---> 
[Newtonsoft.Json.JsonPropertyAttribute("Value")]
public string Value{ get; set; }
您必须删除这些代码行:

[Newtonsoft.Json.JsonPropertyAttribute("000-002")]
public string MajorLoanType { get; set; }

您尝试了什么来解决您的问题?可能有一些奇特的处理程序可以提供给JSON.NET,使其以这种方式工作,但我怀疑,将原始JSON反序列化为具有FieldName和Value属性的对象,然后将其转换为字典,重新序列化该字典可能更简单,然后反序列化到ExportModel。我已经尝试了@StriplingWarrior。我可以使用,但我希望使用属性。此处显示的JSON无效,因为它在
ABC
之后缺少一个引号,并且后面有一个逗号(JSON不允许,即使ES6+允许)。我成功地将其反序列化为具有FieldName和Value属性的类。我尝试的是将FieldName映射到属性并将其值设置为它。欢迎使用Stack Overflow,谢谢您的回答。这是底漆。另外,解释您对代码所做的更改及其原因,不仅可以帮助提出问题的用户,也可以帮助后来的访问者。您可以使用c#generic类和接口来完成这项工作。