如何从JSON对象检索值并使用google-plus:0.1插件进行身份验证

时间:2015-06-18 07:06:00

标签: json grails

我在Grails应用程序中使用google-plus:0.1插件以启用使用Google+登录,我已成功登录并且我正在获取JSON对象。

内部插件有一个代码,他们将JSON字符串转换为JSON对象并返回给我。 这是插件中的代码: -

public Person getCurrentUserProfile() {
    Person person
    try {
        URL url = new URL("https://www.googleapis.com/plus/v1/people/me?access_token=${this.accessToken}")
        def jsonString = JSON.parse(url?.text)
        println "URL : " + url.toString()
        println "JSON : " + jsonString

        person = Person.parseJSONForPerson(jsonString)
    } catch (GooglePlusException e) {
        e.printStackTrace()
    }
    return person
}

从这里我得到了JSON对象 在控制台中打印的Json字符串是: -

URL : https://www.googleapis.com/plus/v1/people/me?access_token=ya29.lgFPSagK5lQ8l19hWuxra8AxsXzZjxlbCetP-aWpUHbq2_djrdvj-3XMFMEgl9r4WfaiLvo7Q3RfJQ
JSON : [etag:"RqKWnRU4WW46-6W3rWhLR9iFZQM/eUT2aym4R39YBu_ra8ZlhztXJ6w", organizations:[[startDate:1995, title:Science, primary:false, name:St.Joseph's Convent, endDate:2008, type:school]], image:[isDefault:false, url:https://lh5.googleusercontent.com/-kuh1_bl_8is/AAAAAAAAAAI/AAAAAAAAADA/ZZfY8ob2QrQ/photo.jpg?sz=50], isPlusUser:true, emails:[[value:kartiki.sahu@crowdera.co, type:account]], kind:plus#person, url:https://plus.google.com/102332421579091283598, id:102332421579091283598, verified:false, name:[familyName:Sahu, givenName:Kartiki], domain:crowdera.co, circledByCount:9, gender:female, displayName:Kartiki Sahu, objectType:person]

我无法从我收到的对象中获取电子邮件。

我尝试将familyName作为 person.name.familyName我得到了它的价值, 但对于givenName,它显示为null,对于电子邮件和组织,它显示空列表,如[]。

我的代码: -

def code = request.getParameter("code")
    String accessToken = accessTokenService.generateAccessToken(code)
    googlePlusService.accessToken = accessToken
    def person = googlePlusService.getCurrentUserProfile()

请帮忙。

2 个答案:

答案 0 :(得分:0)

你可以得到这样的价值。

      String str="""{
 "kind": "plus#person",
 "etag": "\\"RqKWnRU4WW46-6W3rWhLR9iFZQM/eUT2aym4R39YBu_ra8ZlhztXJ6w\\"",
 "gender": "female",
 "emails": [
  {
   "value": "kartiki.sahu@crowdera.co",
   "type": "account"
  }
 ],
 "objectType": "person",
 "id": "102332421579091283598",
 "displayName": "Kartiki Sahu",
 "name": {
  "familyName": "Sahu",
  "givenName": "Kartiki"
 },
 "url": "https://plus.google.com/102332421579091283598",
 "image": {
  "url": "https://lh5.googleusercontent.com/-kuh1_bl_8is/AAAAAAAAAAI/AAAAAAAAADA/ZZfY8ob2QrQ/photo.jpg?sz=50",
  "isDefault": false
 },
 "organizations": [
  {
   "name": "St.Joseph's Convent",
   "title": "Science",
   "type": "school",
   "startDate": "1995",
   "endDate": "2008",
   "primary": false
  }
 ],
 "isPlusUser": true,
 "circledByCount": 9,
 "verified": false,
 "domain": "crowdera.co"
}
"""
        def jsonString  = new JsonSlurper().parseText(str)
        render jsonString.emails.value
    }

我将输出作为以下列表。

['kartiki.sahu@crowdera.co']

答案 1 :(得分:0)

    def code = request.getParameter("code")
    String accessToken = accessTokenService.generateAccessToken(code)
    URL url = new URL("https://www.googleapis.com/plus/v1/people/me?                                                                                                                    
    access_token=${accessToken}")
    def jsonString = JSON.parse(url?.text)
    println "URL : " + url.toString()
    println "JSON : " + jsonString
    println "emails  "+jsonString.emails.value[0]
    println "name  "+jsonString.name.givenName + " " + 
    jsonString.name.familyName