C# automatic property deserialization of JSON

前端 未结 4 703
予麋鹿
予麋鹿 2020-11-28 10:56

I need to deserialize some JavaScript object represented in JSON to an appropriate C# class. Given the nice features of automatic properties, I would prefer having them in t

4条回答
  •  遥遥无期
    2020-11-28 11:45

    You can do this with JavaScriptSerializer found in the System.Web.Script.Serialization namespace:

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    Cat c = serializer.Deserialize(jsonString);
    

    I have POCO objects with automatic properties and this works just fine.

    EDIT: I wrote about JSON Serializers in .NET which compares this serializer with DataContractJsonSerializer.

提交回复
热议问题