java.lang.NumberFormatException:预期的 int,但在第 1 行 8454 处为 0.6

2022-09-02 12:35:47

我正在使用改造库进行演示项目中的调用。

我收到以下错误:

java.lang.NumberFormatException:预期有一个 int,但在第 1 列 8454 路径 $.result.results.ads[2].acres 的第 1 列处为 0.6

我的立场是,这取决于GSON。

我将向您展示它正在捕获的JSON:

   {  
       "ad_id":739580087654,
       "property_type":"site",
       "house_type":"",
       "selling_type":"private-treaty",
       "price_type":"",
       "agreed":0,
       "priority":2,
       "description":"Beautiful elevated 0.6 acre site - zoned residential - and within easy walk to this popular and scenic coastal village\r\n\r\n\r\nthe site area is zoned residential ( i.e. can be constructed on for residential home) and has beautiful coastal views\r\n\r\nSpiddal is an exceptionally popular location , just 8 miles west of Galway City but the area has not been over developed.\r\n\r\nAll services and family amenities are location in the village centre.\r\n\r\n",
       "price":135000,
       "bedrooms":null,
       "bathrooms":null,
       "tax_section":"0",
       "square_metres":0,
       "acres":0.6,   <----------------------TRIPPING UP HERE
       "features":[  
          "Zoned residential",
          "within easy walk of coastal village of Spiddal",
          "with coastal views"
       ],
       "ber_rating":"",
       "ber_code":"",
       "ber_epi":0,             
       "city":"",
       "general_area":"Connemara",
       "postcode":null,
       "latlon_accuracy":1,
       "main_email":"",
       "cc_email":"",
       "auction_address":"",
       "start_date":1384425002,
       "listing_date":1384425002,
       "agreed_date":0,
       "auction_date":0,
       "tags":1
    },

我对改造没有那么经验,所以决定学习和整合这个项目。

有人会有什么建议吗?

我无法控制发送的JSON。


答案 1

尝试使用 或 代替 ; 不是整数,而是小数。请注意,java 会自动将小数解释为 ;浮点数的一个例子是 。floatdoubleint0.6doubles0.6f


答案 2

这是因为解析器期望的是 一个,而它得到的实际值是 。您可以做的是在模型中将该值的类型从 更改为。intfloatintfloat

无论您在何处使用该值,都可能导致代码出现问题。您可以通过将浮点值转换为整数来解决它。


推荐