在json反序列化中将int转换为bool

时间:2021-09-04 02:57:22

I have a class that i uses JavaScriptSerializer.Deserialize<class>

我有一个类,我使用JavaScriptSerializer.Deserialize

One of the properties i get from the UI is an int (0 or 1). Is there any way to change the getter/setter on the property in c# so it understands that it should return true or false based on the value? Or do i need another property that checks the int prop? Thanks

我从UI获得的属性之一是int(0或1)。有没有办法在c#中更改属性的getter / setter,以便它知道它应该根据值返回true或false?或者我需要另一个检查int prop的属性?谢谢

1 个解决方案

#1


3  

The best way would be to create your own JavaScriptConverter which can be used to handle your object and convert between different data types. Override the serialize/deserialize methods and parse the format as you wish.

最好的方法是创建自己的JavaScriptConverter,它可以用来处理你的对象并在不同的数据类型之间进行转换。重写serialize / deserialize方法并根据需要解析格式。

Here's a link to a previous answer I wrote which implements the JavaScriptConverter, showing you how you'd pass it to the JavaScriptSerializer object: https://*.com/a/4999004/298053

这是我之前写的一个实现JavaScriptConverter的答案的链接,向您展示如何将它传递给JavaScriptSerializer对象:https://*.com/a/4999004/298053

#1


3  

The best way would be to create your own JavaScriptConverter which can be used to handle your object and convert between different data types. Override the serialize/deserialize methods and parse the format as you wish.

最好的方法是创建自己的JavaScriptConverter,它可以用来处理你的对象并在不同的数据类型之间进行转换。重写serialize / deserialize方法并根据需要解析格式。

Here's a link to a previous answer I wrote which implements the JavaScriptConverter, showing you how you'd pass it to the JavaScriptSerializer object: https://*.com/a/4999004/298053

这是我之前写的一个实现JavaScriptConverter的答案的链接,向您展示如何将它传递给JavaScriptSerializer对象:https://*.com/a/4999004/298053