Reputation: 11
there is such a JSON: https://restcountries.com/v3.1/all I just want to have a choice "translations" -> "ita" -> "common"
HTTPBuilder getHttpBuilder() {
new HTTPBuilder('https://restcountries.com/')
}
def http = httpBuilder.request(Method.GET, ContentType.JSON){
uri.path = 'v3.1/all'
uri.query = [fields: 'translations,ita,common']
response.success = { resp, json ->
log.error(json.toString()) //string
log.error(JsonOutput.toJson(json).br) //json
log.error(JsonOutput.prettyPrint(JsonOutput.toJson(json))) //formated json
}
}
but I always get either a general view or nothing of what is needed Help me to understand! Thank you!
Upvotes: 1
Views: 226
Reputation: 11
Found this solution for me. Maybe someone else will stumble upon it too.
import groovy.json.JsonOutput
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovy.json.JsonSlurper
HTTPBuilder getHttpBuilder() {
new HTTPBuilder('https://restcountries.com/')
}
def http = httpBuilder.request(Method.GET, ContentType.JSON){
uri.path = 'v3.1/all'
}
def tempJson = JsonOutput.toJson(http)
def resultParseJson = parseJsonText(tempJson)
def needResult = resultParseJson.translations.ita.common
def parseJsonText(String textJson){
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(textJson)
}
needResult - what I need. Perhaps someone will have a more beautiful or correct solution - I will be grateful. But so far this result is satisfactory.
Upvotes: 0