dongying7847 2014-08-19 06:19
浏览 58
已采纳

使用Jquery获取JSON数据馈送,并以HTML格式显示多个json数组对象

I am using jquery and getJSON to GET a data feed constructed by PHP. The Feed displays fine when visiting the PHP page. The problem I am running into is that the JSON feed returns as multiple objects when it is GET requested in jQuery, and I don't know how to write the jQuery to display all objects and their data.

jQuery:

$(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 > );
    });
}

PHP (Constructs JSON Feed):

$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();

Sample JSON Feed:

[{
    "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"
}]
  • 写回答

2条回答 默认 最新

  • doufuhao0566 2014-08-19 06:44
    关注

    Your output is an Array of JSON objects. Fortunately, JavaScript is convenient for manipulating JSON (actually, that is why JSON was created..), and jQuery is convenient for manipulating the DOM.

    To parse the result, you can just iterate through that Array, construct whatever HTML string that you need within the Array, and then insert it into the DOM using jQuery.

    Here a simple example with lists :

    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);
    

    If you only want to display some of the properties, you can read them separately and do additional formatting (for the date for example). It is also useful to give the different HTML objects ids corresponding to what they represent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算