Json-无法将字符串强制转换为java.lang.Integer

时间:2019-06-14 19:29:37

标签: json groovy

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

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

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

Json:

[
  {
    "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

1 个答案:

答案 0 :(得分:0)

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