Php 使用Jquery获取json数据提要,并在HTML中显示多个json数组对象
我正在使用jquery和getJSON获取由PHP构建的数据提要。访问PHP页面时,提要显示良好。我遇到的问题是,JSON提要在jQuery中被请求时会以多个对象的形式返回,我不知道如何编写jQuery来显示所有对象及其数据 jQuery:Php 使用Jquery获取json数据提要,并在HTML中显示多个json数组对象,php,jquery,arrays,json,get,Php,Jquery,Arrays,Json,Get,我正在使用jquery和getJSON获取由PHP构建的数据提要。访问PHP页面时,提要显示良好。我遇到的问题是,JSON提要在jQuery中被请求时会以多个对象的形式返回,我不知道如何编写jQuery来显示所有对象及其数据 jQuery: $(document).ready(function () { $("#display_results").hide(); $("#searchButton").click(function (event) { $("#dis
$(document).ready(function () {
$("#display_results").hide();
$("#searchButton").click(function (event) {
$("#display_results").show();
event.preventDefault();
search_ajax_way();
});
$("#search_results").slideUp();
$("#searchBox").keyup(function (event) {
$("#display_results").show();
});
});
function search_ajax_way() {
//var search_this=$("#searchBox").val();
//$.post("http:/url.com/folder/cl/feed.php", {city : search_this}, function(data){
//$("#display_results").html(data);
//});
var search_this = $("#searchBox").val();
$.getJSON('http://url.com/app/cl/disp_byowner_feed.php', {
city: search_this
}, function (result) {
$.each(result, function (i, r) {
console.log(r.seller);
window.title = r.title;
window.seller = r.seller;
window.loc = r.location;
(Plan on listing all keys listed in the data feed below here)
});
console.log(result);
$("#display_results").html(window.seller + < need to list all keys / values here > );
});
}
示例JSON提要:
$city = 'Kanosh';
$s = "SELECT * FROM `list` WHERE `city` LIKE '%".$city."%'";
$res = $mysqli->query($s) or trigger_error($mysqli->error."[$s]");
$a = array();
while($row = $res->fetch_array(MYSQLI_BOTH)) {
$a[] = array(
'title' => $row['title'],
'price' => $row['price'],
'rooms' => $row['rooms'],
'dimensions' => $row['dimensions'],
'location' => preg_replace('pic\u00a0map', '', $row['location']),
'price' => $row['price'],
'address' => $row['address'],
'seller' => $row['seller'],
'href' => $row['href'],
'date' => $row['date']
);
}
header('Content-Type: application/json');
echo json_encode($a);
$res->free();
$mysqli->close();
[{
"title": "Great Ski-In Location with Seller Financing Available ",
"price": " (Park City near the Resort) ",
"rooms": "",
"dimensions": "",
"location": "",
"address": "Crescent Road at Three Kings Dri",
"seller": "real estate - by owner",
"href": "http:\/\/saltlakecity.craigslist.org",
"date": "20140811223115"
}, {
"title": "Prospector Building 1 - Tiny Condo, Great Location - Owner Financing",
"price": "$75000",
"rooms": false,
"dimensions": "",
"location": " Prospector Square Park City Ut",
"address": "",
"seller": "real estate - by owner",
"href": "http:\/\/saltlakecity.craigslist.org",
"date": "20140811223115"
}]
您的输出是一个JSON对象数组。 幸运的是,JavaScript实际上可以方便地操作JSON,这就是创建JSON的原因,而jQuery可以方便地操作DOM 要解析结果,只需迭代该数组,在数组中构造所需的任何HTML字符串,然后使用jQuery将其插入DOM 下面是一个简单的列表示例:
var html = "";
for (var i = 0; i < result.length; i++) { //assuming result in the JSON array
html += '<ul id = 'house_' + i>';
for (var property in result[i]) { //read all the object properties
html += '<li>' + property + ' : ' + result[i][property] + '</li>';
}
html += '</ul>';
};
$("whatever_div").html(html);
如果只想显示某些属性,可以单独读取它们,并对日期进行其他格式设置(例如)。
给不同的HTML对象ID与它们所代表的内容相对应也很有用。完美答案。非常感谢。
var html = "";
for (var i = 0; i < result.length; i++) { //assuming result in the JSON array
html += '<ul id = 'house_' + i>';
for (var property in result[i]) { //read all the object properties
html += '<li>' + property + ' : ' + result[i][property] + '</li>';
}
html += '</ul>';
};
$("whatever_div").html(html);