【问题标题】:JsonDeserialize is returning null values after attempting to bind to modelJsonDeserialize 在尝试绑定到模型后返回空值
【发布时间】:2019-04-03 19:28:09
【问题描述】:

我正在尝试将 sample.json 中的值链接到名为 PayInfo[] 的模型。我正在使用 Newtonsoft JsonDeserialize,虽然它是

a) 找到正确的路径 (sample.json);和

b) 正确访问我的模型,

它没有将 sample.json 数据的每个值链接到模型。调试时,即使 sample.json 文件中有大量虚拟数据,字段的值也会显示为 null。 请注意,我的目标是反序列化 sample.json 中的数据,将其绑定到模型,并将其显示在我的视图中。

任何提示将不胜感激!

JsonSerializeMethod

 public static List<PayInfo> ReadJson()
{
    // read file into a string and deserialize JSON to a type
    var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonData\sample.json"));
    // deserialize JSON directly from a file
    using (StreamReader file = File.OpenText(@"JsonData\sample.json"))
    {
        JsonSerializer serializer = new JsonSerializer();
        // PayInfo[] payInfoData = (PayInfo[])serializer.Deserialize(file, typeof(PayInfo[]));
        payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
        return payInfoData1;
    }
}

模型

public class PayInfo
{

    public Payee Payee { get; set; }
    public Payment Payment { get; set; }
    public List<Remittance> Remittance { get; set; }

}
public class Payee
{
    [JsonProperty("Name")]
    public string PayeeName { get; set; }

    [JsonProperty("Fax")]
    public string PayeeFax { get; set; }

    [JsonProperty("Phone")]
    public string PayeePhone { get; set; }

    public Address Address { get; set; }

    public string Attention { get; set; }
    public string SubmissionDate { get; set; }
    public string PaymentExp { get; set; }
}


public class Payment
{
    public string PAN { get; set; }
    public string CVV { get; set; }
    public string Exp { get; set; }


}
public class Address
{

    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string StateOrProvince { get; set; }
    public string Zip { get; set; }

}
public class Remittance
{
    public string PayorName { get; set; }
    public string PayorId { get; set; }
    public string InvoiceNo { get; set; }
    public string Description { get; set; }
    public string Amount { get; set; }
}

*这是我的控制器

    public class HomeController : Controller
{
    // GET: /Home/
    [HttpGet]
    [Route("")]
    public IActionResult Index()
    {
        // PayInfo[] payData = JsonToFile<PayInfo[]>.ReadJson();
        // return View(payData);
        PayInfo payList = new PayInfo();
        var wordUp = JsonToFile<payList>.ReadJson();
        return View(payList);
    }
}

来自 sample.json 的 JsonData

   [
  {
    "Payee": {
      "Name": "BLEENDOT",
      "Fax": "(942) 424-2678",
      "Phone": "(980) 494-2960",
      "Address": {
        "Address1": "551 Hoyt Street",
        "Address2": "",
        "City": "Rivera",
        "StateOrProvince": "Ohio",
        "Country": "US",
        "PostalCode": 40529
      },
      "Attention": "Mcdaniel Blankenship",
      "SubmissionDate": "2017-02-06"
    },
    "Payment": {
      "PAN": 1313027774141142,
      "CVV": 723,
      "Exp": "11/2017"
    },
    "Remittance": [
      {
        "PayorName": "Cubix",
        "PayorId": 8314,
        "InvoiceNo": 16981,
        "Description": "Aliquip et aliqua nisi sit sit sint voluptate exercitation quis dolore aute tempor mollit fugiat.",
        "Amount": "$28,192.35"
      },
      {
        "PayorName": "Oceanica",
        "PayorId": 6013,
        "InvoiceNo": 930,
        "Description": "Cillum est est aute aliquip magna occaecat eiusmod labore velit consequat aute occaecat non eu.",
        "Amount": "$76,664.75"
      },
      {
        "PayorName": "Biotica",
        "PayorId": 18461,
        "InvoiceNo": 542,
        "Description": "Exercitation minim ex sint velit amet.",
        "Amount": "$30,718.78"
      }
    ]
  }
]

【问题讨论】:

  • 嗨,欢迎来到 SO。为了让任何人帮助您获得正确的答案,您需要添加代码和相关信息。你提供的还不够。请遵循本指南并更新您的问题。 https://stackoverflow.com/help/mcve
  • 能否也附上示例json文件?也可以试试(IEnumerable&lt;PayInfo&gt;)serializer.Deserialize(file, typeof(IEnumerable&lt;PayInfo&gt;));
  • 已编辑帖子以在文本中包含 sample.json 代码。请让我知道你的想法:)
  • 嘿@nzrytmn,我已经用你的建议更新了代码;现在我的问题是如何将控制器中这个 payInfoData1 的序列化对象发送到视图并在前端解压它?

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


【解决方案1】:

首先,您的属性名称与 json 名称不正确;它们应该相同,否则您必须使用jsonProperty attribute。并且主类名不正确;你的主类应该是一个包含你的子类的类,比如'Payee','Address'和rest,请看我的代码,我修正了一点。

    public class PayInfo
    {

        public Payee Payee { get; set; }
        public Payment Payment { get; set; }
        public List<Remittance> Remittance { get; set; }

    }
    public class Payee
    {
        [JsonProperty("Name")]
        public string PayeeName { get; set; }

        [JsonProperty("Fax")]
        public string PayeeFax { get; set; }

        [JsonProperty("Phone")]
        public string PayeePhone { get; set; }

        public Address Address { get; set; }

        public string Attention { get; set; }
        public string SubmissionDate { get; set; }
        public string PaymentExp { get; set; }
    }


    public class Payment
    {
        public string PAN { get; set; }
        public string CVV { get; set; }
        public string Exp { get; set; }


    }
    public class Address
    {

        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string StateOrProvince { get; set; }
        public string Zip { get; set; }

    }
    public class Remittance
    {
        public string PayorName { get; set; }
        public string PayorId { get; set; }
        public string InvoiceNo { get; set; }
        public string Description { get; set; }
        public string Amount { get; set; }
    }

你可以像这样修改你的方法,使用 list 而不是 array ,会更容易。

     public static List<PayInfo> ReadJson()
    {
        // read file into a string and deserialize JSON to a type
        var payInfoData1 = JsonConvert.DeserializeObject<List<PayInfo>>(File.ReadAllText(@"JsonData\sample.json"));
        // deserialize JSON directly from a file
        using (StreamReader file = File.OpenText(@"JsonData\sample.json"))
        {
            JsonSerializer serializer = new JsonSerializer();
            // PayInfo[] payInfoData = (PayInfo[])serializer.Deserialize(file, typeof(PayInfo[]));
            payInfoData1 = (List<PayInfo>)serializer.Deserialize(file, typeof(List<PayInfo>));
            return payInfoData1;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 2013-08-11
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多