gpt4 book ai didi

c# - 内容 '\/Date(' ')\/' does not start with '\/Date (' and end with ' )\/' 根据 JSON 要求

转载 作者:太空狗 更新时间:2023-10-29 22:15:34 24 4
gpt4 key购买 nike

如您所见,我想向 WCF 休息服务发送 post 请求:

Guid id;
id = Guid.NewGuid();

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:47026/NewsRepository.svc/AddNews");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
id = id,
Subject="wfwf",
ViewerCounter="1",
Content="fsdsd",
SubmitDatatime="2012/12/12",
ModifiedDateTime="2012/12/12",
PublisherName="sdaadasd",
PictureAddress="adfafsd",
TypeOfNews="adsadaad"
});

streamWriter.Write(json);
}
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch(Exception er)
{
MessageBox.Show(er.ToString());
}

但我收到一个错误 - 400 错误请求。所以我跟踪了我的 WCF 日志文件并发现了这个错误:

There was an error deserializing the object of type CMSManagement.Domain.Entity.News. DateTime content '2012/12/12' does not start with '/Date(' and end with ')/' as required for JSON.

在我亲爱的 friend @svek 代码之后。结果是这样的:

Guid id;
id = Guid.NewGuid();

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:47026/NewsRepository.svc/AddNews");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "PUT";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};

string json = JsonConvert.SerializeObject(new
{
id = id,
Subject = "wfwf",
ViewerCounter = "1",
Content = "fsdsd",
SubmitDatatime = "2012/12/12",
ModifiedDateTime = "2012/12/12",
PublisherName = "sdaadasd",
PictureAddress = "adfafsd",
TypeOfNews = "adsadaad"
}, microsoftDateFormatSettings);
streamWriter.Write(json);
}
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch(Exception er)
{
MessageBox.Show(er.ToString());
}

但是我得到了同样的错误。为什么?

最佳答案

使用 JsonConvert

而不是使用

string json = new JavaScriptSerializer().Serialize( new {...} );

使用

//using Newtonsoft.Json;
string json = JsonConvert.SerializeObject(new {...} );

设置日期时间格式

Prior to Json.NET 4.5 dates were written using the Microsoft format: "/Date(1198908717056)/". If you want to use this format, or you want to maintain compatibility with Microsoft JSON serializers or older versions of Json.NET, then change the DateFormatHandling setting to MicrosoftDateFormat.

来源:http://www.newtonsoft.com/json/help/html/DatesInJSON.htm

默认 Json.NET 4.5 格式

// default as of Json.NET 4.5
string isoJson = JsonConvert.SerializeObject(data);
// { "MyDateProperty":"2009-02-15T00:00:00Z" }

Microsoft Date Format

JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};

string microsoftJson = JsonConvert.SerializeObject(data, microsoftDateFormatSettings);
// { "MyDateProperty":"\/Date(1234656000000)\/" }

JavaScript JSON 格式

string javascriptJson = JsonConvert.SerializeObject(data, 
new JavaScriptDateTimeConverter());
// { "MyDateProperty":new Date(1234656000000)}

解决方案代码

这是针对您的问题的完整解决方案:

JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};

string json = JsonConvert.SerializeObject(new
{
id = id,
Subject = "wfwf",
ViewerCounter = "1",
Content = "fsdsd",
SubmitDatatime = "2012/12/12",
ModifiedDateTime = "2012/12/12",
PublisherName = "sdaadasd",
PictureAddress = "adfafsd",
TypeOfNews = "adsadaad"

}, microsoftDateFormatSettings); // ⇦ Added the format argument here

using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(streamWriter, json);
}

关于c# - 内容 '\/Date(' ')\/' does not start with '\/Date (' and end with ' )\/' 根据 JSON 要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44227653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com