C# 反序列化JSON中的DateTime值被编译器视为字符串和DateTime

C# 反序列化JSON中的DateTime值被编译器视为字符串和DateTime,c#,asp.net,asp.net-mvc,json.net,C#,Asp.net,Asp.net Mvc,Json.net,我遇到了一个令人沮丧的问题 我正在反序列化ASP.NET MVC应用程序中的JSON文件。我需要查看日期,但我想对其进行格式化,使其对查看者更友好 在模型中,它是一个日期时间: namespace Company.Name.PRNImportExportUI.Models { public class NPPRFile { public string SubmitterID { get; set; } public DateTime Transac

我遇到了一个令人沮丧的问题

我正在反序列化ASP.NET MVC应用程序中的JSON文件。我需要查看日期,但我想对其进行格式化,使其对查看者更友好

在模型中,它是一个日期时间:

namespace Company.Name.PRNImportExportUI.Models
{
    public class NPPRFile
    {
        public string SubmitterID { get; set; }
        public DateTime TransactionDate { get; set; }
在视图的cshtml中

@using System.IO;
@using Newtonsoft.Json;
@using Aetna.Medicaid.PRNImportExportUI.Models

@{
    ViewBag.Title = "NPPRResponse";
    Layout = "~/Views/Shared/_Layout.cshtml";
    string nppr = File.ReadAllText(@"\\path\to\testnppr.json");
    NPPRFile NpprResponse = JsonConvert.DeserializeObject<NPPRFile>(nppr);
}

<section>
    <h2>NPPRResponse</h2>
    <p>Submitter ID: @NpprResponse.SubmitterID</p>
    <p>Transaction Date: @DateTime.Parse(NpprResponse.TransactionDate)</p>
</section>
@使用System.IO;
@使用Newtonsoft.Json;
@使用Aetna.Medicaid.PRNImportExportUI.Models
@{
ViewBag.Title=“NPPRResponse”;
Layout=“~/Views/Shared/_Layout.cshtml”;
字符串nppr=File.ReadAllText(@“\\path\to\testnppr.json”);
NPPRFile NpprResponse=JsonConvert.DeserializeObject(nppr);
}
NPPRResponse
提交者ID:@NpprResponse.SubmitterID

事务日期:@DateTime.Parse(NpprResponse.TransactionDate)

问题是,如果我试图解析
NpprResponse.TransactionDate
它说它不是一个字符串,而是一个
DateTime
。我得到一个编译器错误,它不会生成。但是如果我调用
NpprResponse.transactionDate.getType()
我会得到
System.lang.string
。如果我调用
NpprResponse.TransactionDate.ToString(“MM DD YYYY”)
它将无法编译,因为它无法对字符串进行日期时间格式化


就在这只奇怪的薛定谔猫身上,它的位置是这样的。为什么不把这个属性当作字符串或日期时间呢?(最好是DateTime,因为它在模型中是这样定义的)

恐怕我怀疑你对
NpprResponse.transactionDate.getType()
返回
System.lang.string
(除此之外,它应该是
System.string
)。调用
NpprResponse.TransactionDate.ToString(“MM DD YYYY”)
时,您会遇到什么准确的错误?请提供一个,理想情况下作为一个控制台应用程序-如果这真的是ASP.NET特定的,我会感到惊讶。错误:CS1503:参数1:无法从'string'转换为'System.ifformatProvider我也懂Java,所以我想这就是“lang”如何潜入
System.string
的原因,但是是的,它说的是
System.string
,如果调用
NpprResponse.TransactionDate.FooBar()
,它显示的确切错误信息是什么?同样,如果您可以在ASP.NET之外的上下文中复制,我们将更容易依次复制它。您是否尝试将其强制转换为日期时间<代码>((DateTime)NpprResponse.TransactionDate).ToString(“MM DD YYYY”)或者将其转换为字符串,然后解析
DateTime.Parse(NpprResponse.TransactionDate.ToString())。ToString(“MM DD YYYY”)
JsonConvert将始终将其设置为日期时间字段。。。这使得@DateTime.Parse(NpprResponse.TransactionDate)变得多余。您应该能够执行NpprResponse.TransactionDate.ToString(“MM DD YYYY”)