使用映射在groovy中迭代JSON对象 import com.avoka.component.http.GetRequest 导入groovy.json.JsonOutput 导入groovy.json.JsonSlurper 导入org.apache.http.client.methods.CloseableHttpResponse 导入org.apache.http.client.methods.HttpGet 导入org.apache.http.client.utils.URIBuilder 导入org.apache.http.impl.client.CloseableHttpClient 导入org.apache.http.impl.client.HttpClients 导入org.apache.http.util.EntityUtils CloseableHttpClient=HttpClients.createDefault(); def uri=新的URIBuilder(“https://randomuser.me/api/?results=30&nat=US") HttpGet请求=新的HttpGet(uri.build()) setHeader(“内容类型”、“应用程序/json”) CloseableHttpResponse response=client.execute(请求); 字符串json=EntityUtils.toString(response.getEntity()); def jsonSlurper=新的jsonSlurper() def object=jsonSlurper.parseText(json) def用户=[:] 对于(int i=0;i [街道:it.Street, 城市:它,城市, 州:它,州, 邮政编码:it.邮政编码] } users.phone=contactJson.phone users.dateofbirth=contactJson.dob.age users.national=contactJson.nat } 打印用户 我正在循环json对象,并尝试使用映射填充响应。 捕获:groovy.lang.MissingPropertyException:没有这样的属性:street for class:java.util.LinkedHashMap$条目
groovy.lang.MissingPropertyException:没有这样的属性:street for class:java.util.LinkedHashMap$条目 在post$\u run\u closure1.doCall(post.groovy:33) at post.run(post.groovy:31) 得到这个错误,我也得到了打印用户中的一个用户,但是列表的大小是30使用映射在groovy中迭代JSON对象 import com.avoka.component.http.GetRequest 导入groovy.json.JsonOutput 导入groovy.json.JsonSlurper 导入org.apache.http.client.methods.CloseableHttpResponse 导入org.apache.http.client.methods.HttpGet 导入org.apache.http.client.utils.URIBuilder 导入org.apache.http.impl.client.CloseableHttpClient 导入org.apache.http.impl.client.HttpClients 导入org.apache.http.util.EntityUtils CloseableHttpClient=HttpClients.createDefault(); def uri=新的URIBuilder(“https://randomuser.me/api/?results=30&nat=US") HttpGet请求=新的HttpGet(uri.build()) setHeader(“内容类型”、“应用程序/json”) CloseableHttpResponse response=client.execute(请求); 字符串json=EntityUtils.toString(response.getEntity()); def jsonSlurper=新的jsonSlurper() def object=jsonSlurper.parseText(json) def用户=[:] 对于(int i=0;i [街道:it.Street, 城市:它,城市, 州:它,州, 邮政编码:it.邮政编码] } users.phone=contactJson.phone users.dateofbirth=contactJson.dob.age users.national=contactJson.nat } 打印用户 我正在循环json对象,并尝试使用映射填充响应。 捕获:groovy.lang.MissingPropertyException:没有这样的属性:street for class:java.util.LinkedHashMap$条目,groovy,Groovy,groovy.lang.MissingPropertyException:没有这样的属性:street for class:java.util.LinkedHashMap$条目 在post$\u run\u closure1.doCall(post.groovy:33) at post.run(post.groovy:31) 得到这个错误,我也得到了打印用户中的一个用户,但是列表的大小是30 无需对位置字段进行收集操作-这不是收集的实例,而是单个映射 没有必要使用所有这些apache的东西 您需
位置字段进行收集
操作-这不是收集的实例,而是单个映射
用户
只是一个映射,在该映射中,您可以在每次迭代中一次又一次地覆盖关键点 import com.avoka.component.http.GetRequest
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.utils.URIBuilder
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
CloseableHttpClient client = HttpClients.createDefault();
def uri = new URIBuilder("https://randomuser.me/api/?results=30&nat=US")
HttpGet request = new HttpGet(uri.build())
request.setHeader("content-type", "application/json")
CloseableHttpResponse response = client.execute(request);
String json = EntityUtils.toString(response.getEntity());
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(json)
def users =[:]
for (int i =0 ; i< object.results.size() ; i++){
def contactJson = object.results[i]
users.gender = contactJson.gender
users.firstname =contactJson.name.first
users.lastname =contactJson.name.last
users.location = contactJson.location.collect { it ->
[Street : it.street,
city : it.city,
state : it.state,
postcode : it.postcode]
}
users.phone =contactJson.phone
users.dateofbirth = contactJson.dob.age
users.nationality =contactJson.nat
}
print users
I am looping the json object and trying to populate the response using maps.
Caught: groovy.lang.MissingPropertyException: No such property: street for class: java.util.LinkedHashMap$Entry
import groovy.json.JsonSlurper
def slurped = new JsonSlurper().parse(new URL("https://randomuser.me/api/?results=30&nat=US"))
slurped
.results
.collect { u ->
[
gender: u.gender,
firstname: u.name.first,
lastname: u.name.last,
location:[
street: u.location.street,
city: u.location.city,
state: u.location.state,
postcode: u.location.postcode
]
]
}