I return an array of array from php to json
我从php返回一个数组数组到json
here's php array
这是php数组
$cities = array();
while($row = mysql_fetch_array($result)){
$cityRow= array('cityNo'=>$row['city_no'], 'cityName'=>$row['city_name']);
$cities[]=$cityRow;
}
echo json_encode($cities);
Here's the json
这是json
$.getJSON("controllers/Customer.controller.php",param,function(result){
// what should I write here to reach the array elements??
});
2 个解决方案
#1
8
You can iterate over the object using .each
:
您可以使用.each迭代对象:
$.getJSON("controllers/Customer.controller.php", param, function(json){
// loop over each object in the array
// 'i' is the index of the object within the array
// 'val' (or this) is the actual object at that offset
$.each(json, function(i, val) {
console.log(val.cityNo); // same as this.cityNo
console.log(val.cityName); // same as this.cityName
});
});
#2
0
according to the documentation on jQuery you can do
根据jQuery的文档,你可以做到
$.getJSON("controllers/Customer.controller.php",param,function(result){ alert(result.cityNo); alert(result.cityName); });
#1
8
You can iterate over the object using .each
:
您可以使用.each迭代对象:
$.getJSON("controllers/Customer.controller.php", param, function(json){
// loop over each object in the array
// 'i' is the index of the object within the array
// 'val' (or this) is the actual object at that offset
$.each(json, function(i, val) {
console.log(val.cityNo); // same as this.cityNo
console.log(val.cityName); // same as this.cityName
});
});
#2
0
according to the documentation on jQuery you can do
根据jQuery的文档,你可以做到
$.getJSON("controllers/Customer.controller.php",param,function(result){ alert(result.cityNo); alert(result.cityName); });