开发者

How to import data of gmail contacts, using javascript Google Contact API, in more detailed format?

I'm using code from this page http://code.google.com/apis/contacts/docs/1.0/developers_guide_js.html to get list of gmail contacts. Actually it works ok, but I get data of name, address, etc like a simple string, with "\n" as separator, for example:

<script type="text/javascript">
    var contactsService;
    var scope = 'https://www.google.com/m8/feeds';

    function setupContactsService() {
      //contactsService = new google.gdata.contacts.ContactsService('exampleCo-exampleApp-1.0');
      contactsService = new 开发者_如何学编程google.gdata.contacts.ContactsService('GoogleInc-jsguide-1.0');
    }

    function getMyContacts() {
      var contactsFeedUri = 'https://www.google.com/m8/feeds/contacts/default/full'; //?max-results=9999&alt=json&v=3.0
      var query = new google.gdata.contacts.ContactQuery(contactsFeedUri);

      setupContactsService();

      contactsService.getContactFeed(query, handleContactsFeed, handleError);

    }

    var handleContactsFeed = function(result) {
      var entries = result.feed.entry;

      for (var i = 0; i < entries.length; i++) {
        var entry = entries[i];
        var addrs  = entry.getPostalAddresses();
        var name   = entry.getTitle();

        // logging
        console.log(addrs[0]);
        console.log(name);

      }
    }

    function handleError(e) {
      alert(e.cause ? e.cause.statusText : e.message);
    }
</script>

it gives me an object where name and address values are simple strings.

Can I get somehow data in like associative array format, where address will contains separate values of street, zip, city, country; and name separate values of first name, last name etc.

Like:

{
     "type": "address",  
     "value":  
       {  
        "street": "Starret 1234",  
        "city": "City name",  
        "stateOrProvince": "ca",  
        "postalCode": "73000",  
        "country": "USA"
     }
},
{
    "type": "name",
    "value":
    {
        "firstName": "Allen",
        "lastName" : "Iverson",
        .....
    }
}

Thanks in advance!


Seems I found an answer, for get more detailed and formatted info need to add additional parameter to contactsFeedUri for google.gdata.contacts.ContactQuery.

This additional parameter is: ?v=3.0 So in my case function will looks like:

function getMyContacts() {
      var contactsFeedUri = 'https://www.google.com/m8/feeds/contacts/default/full?v=3.0&alt=json';
      var query = new google.gdata.contacts.ContactQuery(contactsFeedUri);

      setupContactsService();

      contactsService.getContactFeed(query, handleContactsFeed, handleError);

    }

And for get necessary data I create a simple obj, which can be useful for somebody:

function contactEntry(entry) {
        this.entry = entry;
        this.testEntry = function() {
            alert( 'test entry' )
        };
        this.getFirstName = function() {
            if ((entry.gd$name == null) || (entry.gd$name.gd$givenName == null) || (entry.gd$name.gd$givenName.$t == null)) {
                return '';
            } else {
                return entry.gd$name.gd$givenName.$t;
            }
        };
        this.getLastName = function() {
            if ((entry.gd$name == null) || (entry.gd$name.gd$familyName == null) || (entry.gd$name.gd$familyName.$t == null)) {
                return '';
            } else {
                return entry.gd$name.gd$familyName.$t;
            }
        };
        this.getAdditionalName = function() {
            if ((entry.gd$name == null) || (entry.gd$name.gd$AdditionalName == null) || (entry.gd$name.gd$AdditionalName.$t == null)) {
                return '';
            } else {
                return entry.gd$name.gd$familyName.$t;
            }
        };
        this.getEmail = function() {
            if ((entry.gd$email == null) || (entry.gd$email.length == 0) || (entry.gd$email[0].address == null)) {
                return '';
            } else {
                return entry.gd$email[0].address;
            }
        };
        this.getStreet = function() {
            if (!this._addrExists() || (entry.gd$structuredPostalAddress[0].gd$street == null)) {
                return '';
            } else {
                return entry.gd$structuredPostalAddress[0].gd$street.$t;
            }
        };
        this.getCity = function() {
            if (!this._addrExists() || (entry.gd$structuredPostalAddress[0].gd$city == null)) {
                return '';
            } else {
                return entry.gd$structuredPostalAddress[0].gd$city.$t;
            }
        };
        this.getCountry = function() {
            if (!this._addrExists() || (entry.gd$structuredPostalAddress[0].gd$country == null)) {
                return '';
            } else {
                return entry.gd$structuredPostalAddress[0].gd$country.$t;
            }
        };
        this.getPostcode = function() {
            if (!this._addrExists() || (entry.gd$structuredPostalAddress[0].gd$postcode == null)) {
                return '';
            } else {
                return entry.gd$structuredPostalAddress[0].gd$postcode.$t;
            }
        };
        this.getPhone = function() {
            if ((entry.gd$phoneNumber == null) || (entry.gd$phoneNumber.length == 0) || (entry.gd$phoneNumber[0].$t == null)) {
                return '';
            } else {
                return entry.gd$phoneNumber[0].$t
            }
        };
        this.getOrganization = function() {
            if ((entry.gd$organization == null) || (entry.gd$organization.length == 0) || (entry.gd$organization[0].getOrgName() == null)) {
                return '';
            } else {
                return entry.gd$organization[0].getOrgName().getValue();
            }
        };
        this.getBirthday = function() {
            if ((entry.gContact$birthday == null) || (entry.gContact$birthday.when == null)) {
                return '';
            } else {
                return entry.gContact$birthday.when;
            }
        };
        this.getEvent = function() {
            if ((entry.gContact$event == null) || (entry.gContact$event.length == 0) || (entry.gContact$event[0].gd$when == null)) {
                return '';
            } else {
                return entry.gContact$event[0].gd$when.startTime;
            }
        };
        // protected methods
        this._addrExists = function() {
            if ((entry.gd$structuredPostalAddress == null) || (entry.gd$structuredPostalAddress.length == 0)) {
                return false;
            }

            return true;
        };
    } 

It can be used in this way:

var handleContactsFeed = function(result) {

      var entries = result.feed.entry;

      var contact = new contactEntry(entries[0]);

                var address = {};
        address['fname']   = contact.getFirstName();
        address['lname']   = contact.getLastName() + (contact.getAdditionalName() != '' ? ' ' + contact.getAdditionalName() : '');
        address['address'] = contact.getStreet();
        address['city']    = contact.getCity();
        address['country'] = contact.getCountry();
        address['zip']     = contact.getPostcode();
        address['phone']   = contact.getPhone();
        address['mail']    = contact.getEmail();
        address['organization'] = contact.getOrganization();
        address['birthday'] = contact.getBirthday();
        address['event']   = contact.getEvent();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜