我正在尝试从联系人那里获取电话号码的值,但我要么[对象,对象]返回给我或者根本没有。当我尝试匹配[i] .phoneNumbers时,我得到[object,Object],当我添加匹配[i] .phoneNumbers [0] .value时,我的警报完全停止。
function callme() {
var options = new ContactFindOptions();
options.filter = ""; //leaving this empty will find return all contacts
options.multiple = true; //return multiple results
console.log(options);
var filter = ["displayName", "phoneNumbers"]; //an array of fields to compare against the options.filter
navigator.contacts.find(filter, successFunc, errFunc, options);
function successFunc(matches) {
for (var i = 0; i < matches.length; i++) {
//this loops through all of the contacts
var contact_name = matches[i].displayName;
var contact_number = matches[i].phoneNumbers; //this returns [object, Object]
// var contact_number = matches[i].phoneNumbers[0].value; returns nothing at all
var contact_full = contact_name + " " + contact_number;
}
alert(contact_full);
}
function errFunc() {
alert("oh no!");
}
};
答案 0 :(得分:0)
我不确定您的JSON对象的结构是什么,但您可以在firebug中进行调试,如果您不确定如何执行此操作,请尝试以下操作:
function successFunc(matches) {
for (var i = 0; i < matches.length; i++) {
//this loops through all of the contacts
var contact_name = matches[i].displayName;
alert(JSON.stringify(matches[i].phoneNumbers);
var contact_number = matches[i].phoneNumbers; //this returns [object, Object]
// var contact_number = matches[i].phoneNumbers[0].value; returns nothing at all
var contact_full = contact_name + " " + contact_number;
}
alert(contact_full);
}
警报对话框出现后,您将能够看到对象的属性列表。