unable to resolve class org.json.JSONObject

Pavan
Contributor
January 3, 2023

Hello Team,

I have to use jsonobject in my groovy script.
but compilation is failing

http.request(GET) {
requestContentType = contentType.JSON
response.success = { resp, JSON ->
responseObj = JSON
String body = resp.entity.content.text
JSONObject jsonObject = new JSONObject(body)
log.warn(jsonObject.get("results").length())
if (jsonObject.get("results").length() <= 0){
return false
}
else{
return true
}

}
response.failure = { resp ->
responseObj= [error: "Request failed with status ${resp.status}"]
return false
}
}
could you please suggest any workaround for above problem?

1 answer

1 accepted

1 vote
Answer accepted
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 3, 2023

Hi @Pavan 

Where are you running the script?

You could try adding the import for org.json.JSONObject

I usually use the JsonSlurper class to parse JSON

import groovy.json.JsonSlurper



String jsonString = <some body string>

JsonSlurper slurper = new JsonSlurper()
Map myJson = slurper.parseText(jsonString)
String val = myJson.issue
String val2 = myJson.get("issue")

Suggest an answer

Log in or Sign up to answer