简体   繁体   中英

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. 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.

{
      "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

        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

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. The records is an array which potentially can contain number of objects hence number of Id s.

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:

println json.records.first().Id

Otherwise, this might be better option which will give you Id s of all the objects in records .

println json.records.collect{ it.Id }

@kunal, that helped.

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.

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
        }                   
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM