[英]Access values in JSON based on a condition using Groovy
I am trying to extract two sets of information from the httpResponse (in the form of JSON)- 我正在尝试从httpResponse(以JSON形式)中提取两组信息-
1. Location 1.位置
2. city where fruit = Apple and luckyNumber = 10. 2.水果=苹果且luckyNumber = 10的城市。
{
"userInformation": {
"Name": "John",
"Location": "India"
},
"details": [
{
"fruit": "Apple",
"color": "Red",
"city": "New Delhi",
"luckyNumber": 10
},
{
"fruit": "Banana",
"color": "yellow",
"city": "Goa",
"luckyNumber": 12
}
]
}
For extracting the Location, I tried the below code: 为了提取位置,我尝试了以下代码:
def slurper = new JsonSlurper().parseText(httpResponse)
userLocation = slurper.userInformation.Location
This gives me an error - 这给我一个错误-
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (java.util.LinkedHashMap) values: [[statusCode:200, reason:OK, headers:[Access-Control-Allow-Credential:true, ...], ...]] Possible solutions: parseText(java.lang.String), parse([B), parse([C), parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader)
the error 错误
No signature of method: groovy.json.JsonSlurper.parseText() is applicable for
argument types: (java.util.LinkedHashMap)
Possible solutions: parseText(java.lang.String), ...
means that you try to pass Map ( httpResponse
) into JsonSlurper.parseText()
when this method accepts String. 表示当此方法接受String时,您尝试将Map(
httpResponse
)传递到JsonSlurper.parseText()
。
Find how to get response body as a string and then you can use JsonSlurper.parseText() 查找如何将响应主体作为字符串获取,然后可以使用JsonSlurper.parseText()
Probably you need httpResponse.getData() or just httpResponse.data to access response data payload. 可能您需要httpResponse.getData()或仅需要httpResponse.data来访问响应数据有效负载。 This data may already be in map, if response was parsed correctly based on Content-Type, in this case you do not need to use JsonSlurper.
如果已根据Content-Type正确解析了响应,则此数据可能已经在地图中,在这种情况下,您无需使用JsonSlurper。 If data is a json String then use JsonSlurper.
如果data是json字符串,则使用JsonSlurper。
In any case you will have something like 无论如何你都会有类似的东西
def cities = responseData.details.findAll{it.fruit=="Apple" && it.luckyNumber==10}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.