Error converting JSON to .Net object in asp.net

前端 未结 4 1656
滥情空心
滥情空心 2020-12-08 18:56

I am unable to convert JSON string to .net object in asp.net. I am sending JSON string from client to server using hidden field (by keeping the JSON object.Tostring() in hid

4条回答
  •  無奈伤痛
    2020-12-08 19:53

    Instead of doing this manually I would recommend using the built in lightweight JavaScriptSerializer. No attributes are required on the classes you want to serialize/deserialize.

    It's also more flexible and faster than the DataContractJsonSerializer, since it does not have to care about all the wcf stuff. Additionally it has generic overloads that make it very simple to use AND it can also handle anonymous types.

    Serialization:

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var objectAsJsonString = serializer.Serialize(objectToSerialize);
    

    Deserialization:

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    JsonFeaturedOffer deserializedObject = serializer.Deserialize(s_JsonBaseDate);
    

    To make it even easier you can create Extension methods that will give you json serialization/deserialization directly on the objects/strings.

提交回复
热议问题