I’m trying to deserialize an object which was generated by LinqToSql. The user is allowed to edit the data of the object in the view and then it gets posted back to the controller. The edited Data comes in JSON. How does this action have to look like?
Something like…
public ActionResult(JsonObject json)
{
MyClass c = Jsonify(json) as MyClass;
}
Is there a nice helpful static class in the framework I’m missing? Or do I have to create a DataContract?
Many thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
System.Web.Script.Serialization.JavaScriptSerializer
public ActionResult Blah(JsonObject json)
{
JavaScriptSerializer js = new JavaScriptSerializer();
var c = js.Deserialize<MyClass>(json);
return View(c);
}
EDIT: Oops…just noticed you are passing an object instead of string….so you will need to use System.Runtime.Serialization.Json.DataContractJsonSerializer:
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MyClass)); MyClass c = (MyClass)serializer.ReadObject(json);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0