我设法获得了一个JavaScriptObjects数组,但有时候一个对象的字段中有一个空字符串,当它应该是一个double。
我的叠加类型中的方法与这些类似。
//JSNI methods to get stock data.
public final native double getPrice() /*-{ return this.price; }-*/;
public final native double getChange() /*-{ return this.change; }-*/;
如果该字段确实是double,则JavaScriptObject返回它。
当我在一个JavaScript对象上调用getPrice()时,如果有一个空字符串字段,我会得到一个异常。
我应该如何以及在哪里处理这个?
示例数据: { “年”:1881年, “年平均值”: - 0.2, “5年平均值”:“” }, { “年”:1882年, “年平均值”: - 0.26, “5年平均值”: - 0.27 },
注意:我的程序的这部分主要基于
http://code.google.com/webtoolkit/doc/latest/tutorial/JSON.html http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html
答案 0 :(得分:0)
好吧,如果它是一个空字符串""
,那么它就无法转换为double
类型。您必须在JSNI方法中处理它(显然,如果JSON值为getXXX()
,您可以决定从""
方法返回什么,但假设您要返回0那你的吸气器应该是这样的:
public final native double getPrice() /*-{ return this.price === "" ? 0 : this.price; }-*/;