How does MVC asp.net serialization work for Json object on Controller actions?
How does MVC asp.net serialization work for Json object on Controller actions ?
For example I have a custom object and if send an ajax request with JSON objects to the server action
public ActionResult(List<CustomObject> abc)
{
// Object is serialized automatically how MVC is doing it is the question.
}
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.
public ActionResult(string abc)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<CustomObject> lstabc = serializer.Deserialize<List<Cu开发者_开发知识库stomObject>>(abc);
}
Which I would like to avoid and secondly which are the best libraries for doing JSON object serialization MVC Asp.net ?
The deserialisation is taken care of by the model binder.
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.
There are some links to explanations of this and how to implement a custom model binder in the following SO question:
ASP.Net MVC Custom Model Binding explanation
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
Good luck!
精彩评论