内容'\/Date('')\/'并非以JSON所需的'\/Date('并以')\/'结尾 [英] Content '\/Date('')\/' does not start with '\/Date(' and end with ')\/' as required for JSON
问题描述
我想向WCF休息服务发送发帖请求,如您所见:
I want to send a post request to a WCF rest service as you can see:
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日志文件,发现了这个错误:
But I get an error - 400 bad request. So I traced my WCF log file and I found this error:
反序列化CMSManagement.Domain.Entity.News类型的对象时出错. JSON所需的DateTime内容'2012/12/12'不能以'/Date('开头,以')/'结尾.
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.
在我亲爱的朋友@svek代码之后.结果是这样的:
After my dear friend @svek code. The result is like this:
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());
}
但是我得到了同样的错误.为什么?
But I get the same error. Why?
推荐答案
使用JsonConvert
代替使用
string json = new JavaScriptSerializer().Serialize( new {...} );
使用
//using Newtonsoft.Json;
string json = JsonConvert.SerializeObject(new {...} );
设置DateTime格式
在Json.NET 4.5之前的日期使用Microsoft格式编写:"/Date(1198908717056)/".如果要使用此格式,或者要保持与Microsoft JSON序列化程序或Json.NET的旧版本的兼容性,请更改
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
// 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)}
解决方案代码
这是您问题的完整解决方案:
Solution Code
Here is the full working solution for your question:
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);
}
这篇关于内容'\/Date('')\/'并非以JSON所需的'\/Date('并以')\/'结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!