序列化 – Newtonsoft中的TypeNameHandling需要$type作为第一个属性?
发布时间:2020-09-06 06:07:38 所属栏目:asp.Net 来源:互联网
导读:参见英文答案 Newtonsoft JSON.net deserialization error where fields in JSON change order1个 我的web api中有以下方法 public void Put(string id, [FromBody]IContent value) { //
参见英文答案 >
Newtonsoft JSON.net deserialization error where fields in JSON change order1个
public void Put(string id,[FromBody]IContent value) { //Do stuff } 我正在使用骨干js使用fiddler将以下JSON发送到服务器,值为null: { "id": "articles/1","heading": "Bar","$type": "BrickPile.Samples.Models.Article,BrickPile.Samples" } 但是如果我在JSON对象中首先添加$type属性,反序列化工作正常,请参阅: { "$type": "BrickPile.Samples.Models.Article,BrickPile.Samples","id": "articles/1","heading": "Bar" } 是否可以配置newtonsoft以检查对象中的$type属性而不是第一个属性,还是可以配置主干,以便它始终首先在JSON对象中添加$type属性? 解决方法这将在骨干网中起作用,但我不知道每个浏览器的行为是否相同.基本上,并不能保证每个浏览器都会按照添加的顺序保留项目.MyModel = Backbone.Model.extend({ // ... toJSON: function(){ // build the "$type" as the first parameter var json = {"$type": "BrickPile.Samples.Models.Article,BrickPile.Samples"}; // get the rest of the data _.extend(json,Backbone.Model.prototype.toJSON.call(this)); // send it back,and hope it's in the right order return json; } }); 不过,你最好让NewtonSoft的JSON解串器工作,而不需要它在特定的位置.希望这是可能的. (编辑:哈尔滨站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 从ASP.NET应用程序使用Active Directory时,DirectoryServic
- asp.net – Web部署安装程序(MSI)中没有“IIS 7部署处理程序
- asp.net – Request.Url.AbsoluteUri和重写的URL
- asp.net – 如何将下拉列表添加为gridview项
- 如何设置特定于ASP.NET请求的log4net上下文属性?
- AJAX分页的代码(后台asp.net)
- asp.net-mvc – 在ClaimsIdentity上,BootstrapContext为nul
- asp.net-ajax – Ajax脚本管理器和母版页
- asp.net – 如何将Controller上的Ajax reqest重定向到登录页
- 实体框架 – 等同于.HasOptional在实体框架核心1(EF7)