JSON 反序列化故障排除
我查看了最近有关反序列化的帖子:
但使用根据提供的故障排除方法,我无法解决当前的问题。看来我也不是唯一一个找不到有效方法的人,所以我希望解决方案能够帮助很多人。我什至搜索过 Safari Books Online,总体印象是 JSON 支持仅适用于 Silverlight。
目前,我不考虑迁移到 JSON.net,除非它是实现我的目标的唯一方法:将 JSON 对象发布到 .NET WCF Web 服务并更新数据库中的相应记录。我跟踪了服务监视器跟踪、netmon 跟踪等,它们都表明我的服务端点似乎没有将 JSON 输入传递给我声明的用于解析各个 JSON 属性的方法。
我的操作合约:
//Update Ticket
[OperationContract]
[WebInvoke(Method = "PUT",
UriTemplate = "UpdateTicket",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
TimeTicket UpdateTicket(TimeTicket t);
我的数据合约:
[Serializable]
[DataContract]
public class TimeTicket
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Project { get; set; }
[DataMember]
public int Hours { get; set; }
[DataMember]
public DateTime Date { get; set; }
}
处理 JSON 数据的方法的一部分。它在“where x.ID == t.ID”行失败,因为 t 是一个空对象:
public TimeTicket UpdateTicket(TimeTicket t)
{
MWAEntities db = new MWAEntities();
var tick = (from x in db.TEST_TimeTicket
where x.ID == t.ID
select new TimeTicket
{
ID = x.ID,
Project = x.project,
Hours = x.hours,
Date = x.date
}).FirstOrDefault();
在执行期间,我使用curl(在 Mac OS X 上)发送 JSON 数据,如下所示:
curl -X PUT http://mywebserver:8000/UpdateTicket -d '{"ID":1,"Project":"Project A-edit2","Hours":32,"Date":"\/Date(1301630400000-0400)\/"}' -H "Content-Type:application/json" -v
详细输出:
* About to connect() to mywebserver port 8000 (#0)
* Trying 192.168.210.218... connected
* Connected to mywebserver (192.168.210.218) port 8000 (#0)
> PUT /UpdateTicket HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: mywebserver:8000
> Accept: */*
> Content-Type:application/json
> Content-Length: 85
Netmon 跟踪显示服务器接收到我传递的 JSON。
有没有办法确定我的 JSON 发生了什么以及处理数据的最佳实践?感谢您的教育!
I've reviewed recent posts regarding deserialization:
- WCF not deserializing JSON input
- Generic WCF JSON Deserialization
- Need help with: "The remote server returned an unexpected response: (400) Bad Request."
but using the troubleshooting methods provided, I was unable to solve my current issue. It looks like I'm not the only one having trouble finding a proven method either, so I hope that a solution will help out quite a few individuals. I've even searched Safari Books Online and the general impression I get is that JSON support is for Silverlight only.
At the moment I'm not considering moving to JSON.net unless it is the only way to accomplish my goal: posting a JSON object to a .NET WCF web service and updating the appropriate record in my database. I've followed Service Monitor traces, netmon traces, etc., and they all show that my service endpoint doesn't appear to be passing the JSON input to the method I've declared for parsing out individual JSON properties.
My Operation Contract:
//Update Ticket
[OperationContract]
[WebInvoke(Method = "PUT",
UriTemplate = "UpdateTicket",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
TimeTicket UpdateTicket(TimeTicket t);
My data contract:
[Serializable]
[DataContract]
public class TimeTicket
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Project { get; set; }
[DataMember]
public int Hours { get; set; }
[DataMember]
public DateTime Date { get; set; }
}
A partial for the method that handles the JSON data. It fails at the line "where x.ID == t.ID" because t is a null object:
public TimeTicket UpdateTicket(TimeTicket t)
{
MWAEntities db = new MWAEntities();
var tick = (from x in db.TEST_TimeTicket
where x.ID == t.ID
select new TimeTicket
{
ID = x.ID,
Project = x.project,
Hours = x.hours,
Date = x.date
}).FirstOrDefault();
During execution, I'm using curl (on Mac OS X) to send JSON data as follows:
curl -X PUT http://mywebserver:8000/UpdateTicket -d '{"ID":1,"Project":"Project A-edit2","Hours":32,"Date":"\/Date(1301630400000-0400)\/"}' -H "Content-Type:application/json" -v
Verbose output:
* About to connect() to mywebserver port 8000 (#0)
* Trying 192.168.210.218... connected
* Connected to mywebserver (192.168.210.218) port 8000 (#0)
> PUT /UpdateTicket HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: mywebserver:8000
> Accept: */*
> Content-Type:application/json
> Content-Length: 85
Netmon traces show that the server receives the JSON I'm passing it.
Is there a way to determine what's happening to my JSON and best practices for handling the data? Thanks for the education!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查德_C,嗨。
我认为你的 JSON 有效负载应该是:
t 是你的 TimeTicket (UpdateTicket(TimeTicket t))
Chad_C, Hi.
I think your JSON payload should be:
t being your TimeTicket (UpdateTicket(TimeTicket t))