这是我的json回复:
[{"customer":{"account_id":1,"account_number":null,"alt_contact":null,"alt_phone":null,"alt_phone_number":"","balance":null,"billing_address":"341234 sucka ma cocka","billing_city":"apple","billing_country":"","billing_state":"nj","billing_zip_code":"021231","company_name":null,"contact":null,"created_at":"2012-03-08T06:02:38-08:00","created_by":1,"credit_limit":null,"custom_datetime1":null,"custom_datetime10":null,"custom_datetime2":null,"custom_datetime3":null,"custom_datetime4":null,"custom_datetime5":null,"custom_datetime6":null,"custom_datetime7":null,"custom_datetime8":null,"custom_datetime9":null,"custom_number1":null,"custom_number10":null,"custom_number2":null,"custom_number3":null,"custom_number4":null,"custom_number5":null,"custom_number6":null,"custom_number7":null,"custom_number8":null,"custom_number9":null,"custom_text1":null,"custom_text10":null,"custom_text11":null,"custom_text12":null,"custom_text13":null,"custom_text14":null,"custom_text15":null,"custom_text16":null,"custom_text17":null,"custom_text18":null,"custom_text19":null,"custom_text2":null,"custom_text20":null,"custom_text3":null,"custom_text4":null,"custom_text5":null,"custom_text6":null,"custom_text7":null,"custom_text8":null,"custom_text9":null,"customer_number":151,"customer_source_id":4,"customer_type_id":2,"customer_type_ref_full_name":null,"delivery_method":null,"edit_sequence":null,"email":null,"email_address":"apple@adsfsd.com","fax":"","first_name":null,"full_name":"123 Landscaping","id":431,"is_active":null,"is_statement_with_parent":null,"item_sales_tax_ref_full_name":null,"item_sales_tax_ref_list_id":null,"job_desc":null,"job_end_date":null,"job_projected_end_date":null,"job_start_date":null,"job_status":null,"job_type_ref_full_name":null,"last_name":null,"list_id":null,"mobile":null,"name":"123 Landscaping","pager":null,"parent_id":null,"parent_ref_list_id":null,"password":null,"phone":"234234234","phone_number":"234234234","preferred_payment_method_ref_full_name":null,"price_level_ref_full_name":null,"qb_parent_list_id":null,"resale_number":null,"sales_rep_ref_full_name":null,"sales_tax_code_ref_full_name":null,"sales_tax_code_ref_list_id":null,"service_address":"341234 sucka ma cocka","service_city":"apple","service_country":"","service_state":"nj","service_zip_code":"021231","special_instructions":"","sublevel":null,"tax_id":3,"terms_ref_full_name":null,"time_created":null,"time_modified":null,"total_balance":null,"updated_at":"2012-03-08T06:02:38-08:00","updated_by":null,"which_billing_address":"service","primary_contact":{"account_id":1,"address":null,"city":null,"country":null,"created_at":"2012-03-08T06:02:39-08:00","customer_id":431,"email1":"apple@adsfsd.com","email1_type":null,"email2":null,"email2_type":null,"email3":null,"email3_type":null,"first_name":"1241231","id":1,"last_name":"","note":null,"phone1":"234234234","phone1_type":null,"phone2":null,"phone2_type":null,"phone3":null,"phone3_type":null,"primary":true,"state":null,"title":null,"updated_at":"2012-03-08T06:02:39-08:00","zip_code":null}}}
以下属性有效:
$.ajax({
url: 'filter.json',
dataType: 'json',
success: function(data) {
response($.map(data, function(row) {
alert(row.customer.id);
alert(row.customer.phone_number);
alert(row.customer.service_address);
alert(row.customer.email);
}));
}
});
但是如果我这样做的话:
alert(row.customer.primary_contact.first_name
我收到错误:
row.customer.primary_contact is undefined
答案 0 :(得分:3)
我怀疑数据数组中可能有一个没有primary_contact
属性的元素。在示例中,您已经证明并非如此,它的工作方式如此live demo所示(事实上,您显示的示例不是有效的JSON,因为您最后没有关闭]
)。
现在,如果在您的JSON结构中您可以拥有没有此属性的记录,您可以在尝试访问它之前先检查其存在:
if (row.customer.primary_contact) {
alert(row.customer.primary_contact.first_name);
}
当然,customer
属性也是如此。