2013-03-21 88 views
0

如何使用Jquery從json格式中提取'customers_firstname'?Jquery Json提取節點?

<?php echo json_encode($users) ?> 

輸出

[{"Customer":{"id":"13","customers_gender":"M","resumetest":"a15ff2aa27d616636b5bc30fac4dba38733f4e5f","created":"2013-01-29 13:49:06","modified":"2013-03-14 11:50:18","customers_firstname":"Gerald","customers_lastname":"Russ"}},{"Customer":{"id":"14","customers_gender":"F","resumetest":null,"created":"2013-01-29 15:41:23","modified":"2013-02-14 15:08:42","customers_firstname":"Jim","customers_lastname":"Carrie"}}] 

回答

1

試試這個:

DEMO

HTML

<ul id="nameList"> 
</ul> 

JS

var customers = [{"Customer":{"id":"13","customers_gender":"M","resumetest":"a15ff2aa27d616636b5bc30fac4dba38733f4e5f","created":"2013-01-29 13:49:06","modified":"2013-03-14 11:50:18","customers_firstname":"Gerald","customers_lastname":"Russ"}},{"Customer":{"id":"14","customers_gender":"F","resumetest":null,"created":"2013-01-29 15:41:23","modified":"2013-02-14 15:08:42","customers_firstname":"Jim","customers_lastname":"Carrie"}}]; 

$.each(customers, function (index, item) { 

     $("#nameList").append("<li>" + item.Customer.customers_firstname + "</li>"); 
    }); 
+0

謝謝隊友!請問? console.log()做了什麼?我如何將結果追加到表格中,並在結果中循環顯示。 – DANLEE 2013-03-21 03:25:00

+0

對於console.log()檢查這個http://stackoverflow.com/questions/4743730/what-is-console-log-and-how-do-i-use-it。更新 :) – Mate 2013-03-21 03:36:39

0

您可以在AJAX成功塊使用下面的代碼

$.each(json_data, function (index, customer){//here json_data is success response from the Server 
if (customer){ 
console.log(customer["customers_firstname"]; 
} 
}) 
+0

怎麼辦你參考?你能建議嗎? – DANLEE 2013-03-21 03:14:48

+0

是否表示您想在某些變量中存儲客戶名稱? – Raghuveer 2013-03-21 03:33:48