C# 在进行json反序列化时解析int

C# 在进行json反序列化时解析int,c#,C#,我正在编写一个自定义javascript转换器,并收到一个应该包含int的字符串。 这就是我正在做的: public class MyObjectToJson : JavaScriptConverter { public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { MyObject TheObje

我正在编写一个自定义javascript转换器,并收到一个应该包含int的字符串。 这就是我正在做的:

public class MyObjectToJson : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{

  MyObject TheObject = new MyObject; 

  if (serializer.ConvertToType<int>(dictionary["TheInt"]) == true)
  {
    MyObject.TheInt = serializer.ConvertToType<int>(dictionary["TheInt"]);
  }
公共类MyObjectToJson:JavaScriptConverter
{
公共重写对象反序列化(IDictionary dictionary、类型、JavaScriptSerializer序列化程序)
{
MyObject TheObject=新的MyObject;
if(serializer.ConvertToType(dictionary[“TheInt”])==true)
{
MyObject.TheInt=序列化程序.ConvertToType(字典[“TheInt”]);
}
但是,它对条件语句不起作用。我需要更改什么?我想测试我是否得到一个int


谢谢。

这是因为ConvertToType返回请求类型的对象。要将其用作
if
子句的条件,它必须返回
bool

您可以改为:

try {
    MyObject.TheInt = serializer.ConvertToType<int>(dictionary["TheInt"]);
}
catch(Exception e)
{
    throw new Exception("Could not convert value into int: " + dictionary["TheInt"]);
}
试试看{
MyObject.TheInt=序列化程序.ConvertToType(字典[“TheInt”]);
}
捕获(例外e)
{
抛出新异常(“无法将值转换为int:+dictionary[“TheInt”]);
}

EDIT:之前我建议检查转换后的值是否为null相等,但意识到当类型不匹配时,该方法抛出异常的可能性大于返回null。

这是因为ConvertToType返回请求类型的对象。要将其用作
if
子句的条件,必须eturn
bool

您可以改为:

try {
    MyObject.TheInt = serializer.ConvertToType<int>(dictionary["TheInt"]);
}
catch(Exception e)
{
    throw new Exception("Could not convert value into int: " + dictionary["TheInt"]);
}
试试看{
MyObject.TheInt=序列化程序.ConvertToType(字典[“TheInt”]);
}
捕获(例外e)
{
抛出新异常(“无法将值转换为int:+dictionary[“TheInt”]);
}

编辑:早些时候,我建议检查转换后的值是否为null相等,但意识到当类型不匹配时,该方法抛出异常的可能性大于返回null的可能性。

更改代码以使用此条件:

 int value;
 if (int.TryParse(serializer.ConvertToType<string>(dictionary["TheInt"]), out value)
 {
    MyObject.TheInt = value;
 }
int值;
if(int.TryParse(serializer.ConvertToType(dictionary[“TheInt”]),out值)
{
MyObject.TheInt=值;
}

这是一个比依赖要抛出的异常更好的解决方案,因为捕获异常在计算上非常昂贵。

更改代码以使用此条件:

 int value;
 if (int.TryParse(serializer.ConvertToType<string>(dictionary["TheInt"]), out value)
 {
    MyObject.TheInt = value;
 }
int值;
if(int.TryParse(serializer.ConvertToType(dictionary[“TheInt”]),out值)
{
MyObject.TheInt=值;
}

这是一个比依赖要抛出的异常更好的解决方案,因为捕获异常在计算上非常昂贵。

如果您不确定类型不能是int,请改用


如果不确定类型不能是int,请改用


ConvertToType
返回的是
int
,而不是
bool
。如果转换成功,它将返回一个请求类型的对象。然后,您将一个对象与布尔值进行比较。
我正在编写一个自定义javascript转换器
为什么?@L.B:因为我想测试一下是否获得了正确的值类型你想验证什么?真的需要通过数据类型来验证吗?
ConvertToType
返回的是
int
,而不是
bool
。如果转换成功,它将返回请求类型的对象。然后,你将对象与布尔值进行比较。
我正在编写一个自定义javascript转换器
为什么?@L.B:因为我ant要测试我是否获得了正确的值类型您想验证什么?真的需要按数据类型进行验证吗?@Doug:如果不能进行转换,ConvertToType()会给出一个异常。为什么要再次执行此操作?只是为了给出一个更有意义的消息。也许我的消息不是这样,而是一个描述应用程序中发生了什么的消息(而不仅仅是告诉您该值无法转换)将有助于排除故障。@Doug:如果无法进行转换,ConvertToType()将给出一个异常。为什么要再次执行此操作?只是为了给出一条更有意义的消息。也许我的消息不是这样,而是一条描述应用程序中发生了什么的消息(而不仅仅是告诉您该值无法转换)将有助于故障排除。