如何使用 jQuery 从 Google 读取联系人信息?

How to read contacts info from Google with jQuery?

我正在使用 Google 的 API 来获取我的联系人列表的信息,并在使用控制台日志后获取信息

    function fetch(token) {
    $.ajax({
      url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&alt=json",
      dataType: "JSON",
      success:function(data) {

        var json = JSON.stringify(data);

        console.log(json);
      }
    });
  } 

我正在获取此信息

{
"version": "1.0",
"encoding": "UTF-8",
"feed": {
    "xmlns": "http://www.w3.org/2005/Atom",
    "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
    "xmlns$batch": "http://schemas.google.com/gdata/batch",
    "xmlns$gd": "http://schemas.google.com/g/2005",
    "xmlns$gContact": "http://schemas.google.com/contact/2008",
    "id": {
        "$t": "sifo.contacto@gmail.com"
    },
    "updated": {
        "$t": "2016-02-22T19:33:36.064Z"
    },
    "category": [{
        "scheme": "http://schemas.google.com/g/2005#kind",
        "term": "http://schemas.google.com/contact/2008#contact"
    }],
    "title": {
        "type": "text",
        "$t": "Sifo Sistema Inteligente de Facturación Online's Contacts"
    },
    "link": [{
        "rel": "alternate",
        "type": "text/html",
        "href": "http://www.google.com/"
    }, {
        "rel": "http://schemas.google.com/g/2005#feed",
        "type": "application/atom+xml",
        "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full"
    }, {
        "rel": "http://schemas.google.com/g/2005#post",
        "type": "application/atom+xml",
        "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full"
    }, {
        "rel": "http://schemas.google.com/g/2005#batch",
        "type": "application/atom+xml",
        "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/batch"
    }, {
        "rel": "self",
        "type": "application/atom+xml",
        "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full?alt=json&max-results=25"
    }, {
        "rel": "next",
        "type": "application/atom+xml",
        "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full?alt=json&start-index=26&max-results=25"
    }],
    "author": [{
        "name": {
            "$t": "Sifo Sistema Inteligente de Facturación Online"
        },
        "email": {
            "$t": "sifo.contacto@gmail.com"
        }
    }],
    "generator": {
        "version": "1.0",
        "uri": "http://www.google.com/m8/feeds",
        "$t": "Contacts"
    },
    "openSearch$totalResults": {
        "$t": "704"
    },
    "openSearch$startIndex": {
        "$t": "1"
    },
    "openSearch$itemsPerPage": {
        "$t": "25"
    },
    "entry": [{
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/1600788a7596f9"
                },
                "updated": {
                    "$t": "2015-10-08T19:48:21.315Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": "Lic. Jose Lugo"
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/1600788a7596f9/1B2M2Y8AsgTpgAmY7PhCfg"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/1600788a7596f9"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/1600788a7596f9/1444333701315001"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "jose.lugo@mecrg.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/243862896da102"
                },
                "updated": {
                    "$t": "2016-02-11T22:58:30.580Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": ""
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/243862896da102/qPx9RZ6Tppge180vMVnHUw"
                }, {
                    "rel": "http://schemas.google.com/contacts/2008/rel#photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/243862896da102"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/243862896da102"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/243862896da102/1455231510580001"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "sharai.alvarez@gmail.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/406bac09eee821"
                },
                "updated": {
                    "$t": "2016-02-11T16:26:46.318Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": ""
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/406bac09eee821/1B2M2Y8AsgTpgAmY7PhCfg"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/406bac09eee821"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/406bac09eee821/1455208006318001"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "rosyalonso3@hotmail.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/48a21c08062798"
                },
                "updated": {
                    "$t": "2016-02-18T21:08:19.835Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": ""
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/48a21c08062798/1B2M2Y8AsgTpgAmY7PhCfg"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/48a21c08062798"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/48a21c08062798/1455829699835001"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "joseluisgr15@yahoo.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/596ac60cfe288f"
                },
                "updated": {
                    "$t": "2015-06-10T21:01:08.157Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": "FLORA EXOTICA DE CHIAPAS"
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/596ac60cfe288f/1B2M2Y8AsgTpgAmY7PhCfg"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/596ac60cfe288f"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/596ac60cfe288f/1433970068157001"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "tropiflores@hotmail.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/69c8d608c00f66"
                },
                "updated": {
                    "$t": "2016-01-07T17:00:01.946Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": ""
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/69c8d608c00f66/bc1NAkUY9UZ5B9Ih8I16dw"
                }, {
                    "rel": "http://schemas.google.com/contacts/2008/rel#photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/69c8d608c00f66"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/69c8d608c00f66"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/69c8d608c00f66/1452186001946000"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "Omarpuribe@gmail.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/8640870e6f72d7"
                },
                "updated": {
                    "$t": "2016-01-20T15:54:41.842Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": ""
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/8640870e6f72d7/1B2M2Y8AsgTpgAmY7PhCfg"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/8640870e6f72d7"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/8640870e6f72d7/1453305281842002"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "aguixcor@gmail.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/adb5708dff4555"
                },
                "updated": {
                    "$t": "2016-01-07T20:46:02.243Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": ""
                },
                "link": [{
                    "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                    "type": "image/*",
                    "href": "https://www.google.com/m8/feeds/photos/media/sifo.contacto%40gmail.com/adb5708dff4555/1B2M2Y8AsgTpgAmY7PhCfg"
                }, {
                    "rel": "self",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/adb5708dff4555"
                }, {
                    "rel": "edit",
                    "type": "application/atom+xml",
                    "href": "https://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/full/adb5708dff4555/1452199562243000"
                }],
                "gd$email": [{
                    "rel": "http://schemas.google.com/g/2005#other",
                    "address": "Ponyjr12@hotmail.com",
                    "primary": "true"
                }]
            }, {
                "id": {
                    "$t": "http://www.google.com/m8/feeds/contacts/sifo.contacto%40gmail.com/base/e5831f8fe0aeb2"
                },
                "updated": {
                    "$t": "2016-02-11T18:22:13.253Z"
                },
                "category": [{
                    "scheme": "http://schemas.google.com/g/2005#kind",
                    "term": "http://schemas.google.com/contact/2008#contact"
                }],
                "title": {
                    "type": "text",
                    "$t": ""
                },
                "link": [{
                            "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
                            "type": "image/*",
                            "href": "https://www.google.com/m8/feeds/photos/media/sifo.c

我想读取 "address" 的值来获取电子邮件地址,但我不知道如何,我尝试使用 $.each 但每次的值都是 "undefined",我应该怎么做才能收到电子邮件?

你到底想在什么上调用 $.each?在整个 data 对象上,或某些 属性?

如果您只想获取电子邮件地址列表,可以使用 map 函数遍历 data.feed.entry 数组并提取嵌套的 gd$email[0].address 值,如下所示:

var emailAddresses = data.feed.entry.map(function(entry) {
    //take the first gd$email item the entry has
    var gdEmail = entry['gd$email'][0];
    //this assumes all entries will have a gd$email, 
    //  you may want to implement a null check
    return gdEmail.address;
});