Error converting JSON to .Net object in asp.net

Vinay Kumar Chella提出了一个问题:Error converting JSON to .Net object in asp.net,或许与您遇到的问题类似。

回答者ntziolis给出了该问题的处理方式:

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<JsonFeaturedOffer>(s_JsonBaseDate);

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

希望本文对你有帮助,欢迎支持JavaScript中文网