Deserialize JSON Objects in Asp.Net MVC Controller

前端 未结 1 682
无人及你
无人及你 2021-01-18 09:43

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 con

相关标签:
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);
    
    0 讨论(0)
提交回复
热议问题