[英]Access to JSON element in groovy map
I'm trying to access an element of some json returned into a map from an api call so I can pass it to another api call. 我正在尝试访问通过api调用返回到地图的json元素,因此我可以将其传递给另一个api调用。 I can't seem to properly create a varible and give it the value I need.
我似乎无法正确创建一个变量并为其提供所需的值。 Here's the returned json, I need to access the Id element.
这是返回的json,我需要访问Id元素。
{
"totalSize": 1,
"done": true,
"records": [
{
"attributes": {
"type": "User",
"url": "/services/data/v24.0/sobjects/User/MYIDNUMBER"
},
"Id": "MYIDNUMBER"
}
]
}
here's the restful service call I use and my attempt to access the Id element and put it in sfId so I can use it in my next API call 这是我使用的宁静服务调用,以及尝试访问Id元素并将其放入sfId的尝试,因此可以在下一个API调用中使用它
def http = new HTTPBuilder(instance_domain)
http.request(GET,JSON) { req ->
uri.path = "services/data/v24.0/query/"
uri.query = [q:"SELECT Id from User WHERE Email = '$loginid@myschool.edu'"]
headers['Authorization'] = "Bearer $access_token"
response.success = { resp, json ->
json.each{ key,value ->
sfMap = [sfUser: [json: json]]
}
sfId = sfMap[records.Id]
}
response.failure = { resp, json ->
println resp.status
println json.errorCode
println json.message
}
}
I get the following error on the server where the portletized version of this is deployed 我在此服务器的portletized版本所在的服务器上收到以下错误
2014-07-08 08:02:39,710 ERROR [http-bio-443-exec-161] portal-web.docroot.html.portal.render_portlet_jsp:154 groovy.lang.MissingPropertyException: No such property: records for class: groovyx.net.http.HTTPBuilder$RequestConfigDelegate
Based on your json structure, here's what I can say. 根据您的json结构,这就是我可以说的。 The
records
is an array which potentially can contain number of objects hence number of Id
s. records
是一个数组,可能包含多个对象,因此可能包含Id
。
def json = new groovy.json.JsonSlurper().parseText ("""
{
"totalSize": 1,
"done": true,
"records": [
{
"attributes": {
"type": "User",
"url": "/services/data/v24.0/sobjects/User/MYIDNUMBER"
},
"Id": "MYIDNUMBER"
}
]
}
""")
If you are sure about first element's Id, then this will do the trick: 如果您确定第一个元素的ID,则可以解决问题:
println json.records.first().Id
Otherwise, this might be better option which will give you Id
s of all the objects in records
. 否则,这可能是一个更好的选择,它将为您提供
records
中所有对象的Id
。
println json.records.collect{ it.Id }
@kunal, that helped. @kunal,有帮助。
For future refernce, here's how I added the delcaration of a varible to be used later on, assigning it the value from the json responce. 为了将来参考,这是我添加了稍后要使用的变量的代用方式,并从json响应中为其分配值的方式。
def http = new HTTPBuilder(instance_domain)
http.request(GET,JSON) { req ->
uri.path = "services/data/v24.0/query/"
uri.query = [q:"SELECT Id from User WHERE Email = '$loginid@myschool.edu'"]
headers['Authorization'] = "Bearer $access_token"
response.success = { resp, json ->
sfId = json.records.first().Id <----- this did the trick
json.each{ key,value ->
sfMap = [sfUser: [json: json]]
}
}
response.failure = { resp, json ->
println resp.status
println json.errorCode
println json.message
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.