我想向 WCF 休息服务发送一个发布请求,如您所见:
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 日志文件,我发现了这个错误:
反序列化类型为 CMSManagement.Domain.Entity.News 的对象时出错。日期时间内容"2012/12/12"不以"/Date("开头,以"(/"结尾,这是 JSON 所要求的。
在我亲爱的朋友@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 {...} );
设置日期时间格式
在 Json.NET 之前,4.5 日期使用Microsoft格式编写:"/Date(1198908717056(/"。如果要使用此格式,或者要保持与Microsoft JSON 序列化程序或旧版本的 Json.NET 的兼容性,请将"日期格式处理"设置更改为 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日期格式
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);
}