首页 > 解决方案 > Json - 字符串不能转换为 java.lang.Integer

问题描述

尝试将 json 值 (-3) 与列表值 (-3) 进行比较时出现以下错误。该代码适用于正 json 值:

错误:java.lang.ClassCastException:java.lang.String 无法转换为 java.lang.Integer

我尝试将值转换为字符串、整数等。每次我得到相同的错误。

杰森:

[
  {
    "Name”:”Delayed”,
    "ID":-3,
    "Ascending":true,
    "LocalDateTime":"2019-06-14T07:00:00+09:00",
    "EpochDateTime":1560463200,
    "Value":10.0,
    "Category":"Very Unlikely",
    "CategoryValue":5,
  }
]

代码:

for (item in indiceId) {
   GlobalVariable.indice_id = item
   idResponse = WS.sendRequest(findTestObject('Object Repository/API/itemsAPI/items_location_id'))
   parsedJsonId = new groovy.json.JsonSlurper().parseText(idResponse.getResponseBodyContent())
   if (parsedJsonId[0].ID == item) {
       KeywordUtil.markPassed(" Items Response by ID: " + parsedJsonId[0].ID  + " Expected: " + item)
   }
   else {
       KeywordUtil.markFailed(" Items Response by ID: " + parsedJsonId[0].ID  + " Expected: " + item)
   }
}

错误:java.lang.ClassCastException:java.lang.String 无法转换为 java.lang.Integer

标签: jsongroovy

解决方案


parsedJsonId = Integer.parseInt(new groovy.json.JsonSlurper().parseText(idResponse.getResponseBodyContent()))


推荐阅读