Arrays 如何从json_编码数组中选择元素?

Arrays 如何从json_编码数组中选择元素?,arrays,object,json,Arrays,Object,Json,我使用JSON和javascript从连接到数据库的php文件中检索数据。这就是我的php代码的样子: while($row = $stmt->fetch_assoc()) { $data[] = $row; } header("Content-type: text/plain"); echo json_encode($data,true); 这是我的Javascript: var displayfeeds = new ajaxObject('ajax/users/display.fe

我使用JSON和javascript从连接到数据库的php文件中检索数据。这就是我的php代码的样子:

while($row = $stmt->fetch_assoc()) {
$data[] = $row;
}

header("Content-type: text/plain");
echo json_encode($data,true);
这是我的Javascript:

var displayfeeds = new ajaxObject('ajax/users/display.feeds.php');
displayfeeds.callback = function (responseText,status) {
    var feed = JSON.parse(JSON.stringify(responseText));
    $("#feeds-feedback").html(feed);
}
displayfeeds.update();
当我运行此命令时,它会打印出如下数组:

[
    {
        "userID":"39160902151",
        "content":"bar bar bar bar",
        "published":"2011-06-07 10:33:35"
    },
    {
        "userID":"5896858666",
        "content":"foo foo foo foo foo",
        "published":"2011-06-06 22:54:51"
    }
]
displayfeeds.callback = function (responseText,status) {
    var feed = JSON.parse(JSON.stringify(responseText));
    var html = '';

    // build html for each feed item
    for (var i = 0; i < feed.length; i++) {

        html += '<h3>UserID:: '+ feed[i]['userID'] +'</h3>';
        html += '<p>'+ feed[i]['content'] +'</p>';
        html += '<div><strong>published</strong>: '+ feed[i]['userID'] +'</div>';
        html += '<hr />';
    }
    // append content once all html is built
    $("#feeds-feedback").append(html);
}
我的问题是:然后如何显示该文件中的用户ID和内容? 我真的很挣扎。我是JSON新手。

要获取第一个用户id:

var feed = JSON.parse(responseText);
feed[0].userID
要获取第一个用户id,请执行以下操作:

var feed = JSON.parse(responseText);
feed[0].userID

您需要将json对象转换为html以显示它们。因此,在回调函数中,我将执行如下操作:

[
    {
        "userID":"39160902151",
        "content":"bar bar bar bar",
        "published":"2011-06-07 10:33:35"
    },
    {
        "userID":"5896858666",
        "content":"foo foo foo foo foo",
        "published":"2011-06-06 22:54:51"
    }
]
displayfeeds.callback = function (responseText,status) {
    var feed = JSON.parse(JSON.stringify(responseText));
    var html = '';

    // build html for each feed item
    for (var i = 0; i < feed.length; i++) {

        html += '<h3>UserID:: '+ feed[i]['userID'] +'</h3>';
        html += '<p>'+ feed[i]['content'] +'</p>';
        html += '<div><strong>published</strong>: '+ feed[i]['userID'] +'</div>';
        html += '<hr />';
    }
    // append content once all html is built
    $("#feeds-feedback").append(html);
}

您需要将json对象转换为html以显示它们。因此,在回调函数中,我将执行如下操作:

[
    {
        "userID":"39160902151",
        "content":"bar bar bar bar",
        "published":"2011-06-07 10:33:35"
    },
    {
        "userID":"5896858666",
        "content":"foo foo foo foo foo",
        "published":"2011-06-06 22:54:51"
    }
]
displayfeeds.callback = function (responseText,status) {
    var feed = JSON.parse(JSON.stringify(responseText));
    var html = '';

    // build html for each feed item
    for (var i = 0; i < feed.length; i++) {

        html += '<h3>UserID:: '+ feed[i]['userID'] +'</h3>';
        html += '<p>'+ feed[i]['content'] +'</p>';
        html += '<div><strong>published</strong>: '+ feed[i]['userID'] +'</div>';
        html += '<hr />';
    }
    // append content once all html is built
    $("#feeds-feedback").append(html);
}

非常感谢你。成功了。我只需要删除stringify并遵循你们两人的建议。谢谢:-非常感谢。成功了。我只需要删除stringify并遵循你们两人的建议。谢谢:-没有必要对JSON进行双重编码,去掉JSON.stringify调用。strigify是与PHP命令JSON_encode等效的JavaScript。因为它是作为字符串接收的,所以不需要在JS中执行。JSON.parse接受一个JSON字符串并将其转换为一个对象,PHP有一个名为JSON_decode的等效函数,如果将JSON字符串传递回PHP,您将使用该函数。无需对JSON进行双重编码,去掉JSON.stringify调用。strigify是与PHP命令JSON_encode等效的JavaScript。因为它是作为字符串接收的,所以不需要在JS中执行。JSON.parse接受一个JSON字符串并将其转换为一个对象,PHP有一个名为JSON_decode的等效函数,如果您将JSON字符串传递回PHP,您将使用该函数。您应该在每个答案的分数下面看到一个灰色复选标记。单击该选项可选择该答案。您应该会在每个答案的分数下方看到一个灰色复选标记。单击该选项以选择该答案。