如何MVC asp.net系列化工作有关控制器操作JSON对象? [英] How does MVC asp.net serialization work for Json object on Controller actions?
问题描述
如何对控制器的操作JSON对象MVC asp.net序列化工作?
How does MVC asp.net serialization work for Json object on Controller actions ?
例如我有一个自定义的对象,如果与JSON对象发送一个Ajax请求到服务器动作
For example I have a custom object and if send an ajax request with JSON objects to the server action
公众的ActionResult(列表< CustomObject> ABC)
{
   //对象自动序列MVC是怎么做起来的问题。
}
为什么我问这个的原因是,我的一些对象无法正常serialzed,因此数据丢失是存在的,那我也恢复到字符串值serializaiton的老方法。
The reason why I am asking this is that some objects of mine are not properly serialzed and hence loss of data is there , then I have to revert to the old method of string value to serializaiton.
公众的ActionResult(字符串ABC)
{
   串行的JavaScriptSerializer =新的JavaScriptSerializer();
   清单< CustomObject> lstabc = serializer.Deserialize<名单,LT; CustomObject>>(ABC);
}
,我想,以避免其次这是最好的图书馆做JSON对象序列化MVC Asp.net?
Which I would like to avoid and secondly which are the best libraries for doing JSON object serialization MVC Asp.net ?
推荐答案
该deserialisation由模型绑定的照顾。
The deserialisation is taken care of by the model binder.
如果你发现它不工作,你可以创建自己的自定义模型绑定与构架注册它 - 这应该意味着你能避免在控制器的所有deserialisation噪音。
if you are finding it is not working you can create your own custom model binder and register it with the framework - this should mean you can avoid all that deserialisation noise in your controllers.
有一些链接的这个解释和如何实现以下SO问题的自定义模型绑定:
There are some links to explanations of this and how to implement a custom model binder in the following SO question:
<一个href=\"http://stackoverflow.com/questions/1193453/asp-net-mvc-custom-model-binding-explanation\">ASP.Net MVC自定义模型绑定解释
一个比较流行的JSON序列化图书馆是newtonsoft json.net库 - 我用它有效地多次:
One of the more popular json serialisation libraries is the newtonsoft json.net library - I've used it effectively many times:
http://james.newtonking.com/pages/json-net.aspx
祝你好运!
这篇关于如何MVC asp.net系列化工作有关控制器操作JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!