php - 如何读取 json 响应

标签 php javascript jquery json getjson

我有一个数据形式的 json 响应,格式如下。它是整个数据发布的一部分

JSON Data Here

我做了一个读取数据的脚本:

$(document).ready(function() {
$.getJSON('http://services.berthojoris.com/json/baca2.php', function(data) {
       for(var i=0; i<data[0].actionDetails.length;i++){
          document.write('Judul Halaman : '+ data[1].actionDetails[i].pageTitle+"<br>");
          document.write('URL : '+ data[1].actionDetails[i].url+"<hr>");
       }
   });
});

但是使用getJSON生成的数据只有1、2或5条数据。虽然实际上有很多结果产生的过程。

Result

如何捕获生成的所有数据?

我只是想从数据中取出pageTitleurl数据。

最佳答案

乍一看,您混合了 data[0]data[1](它将显示 data 中第一项的所有操作详细信息数组)

$(document).ready(function() {
$.getJSON('http://services.berthojoris.com/json/baca2.php', function(data) {
       for(var i=0; i<data[0].actionDetails.length;i++){
          document.write('Judul Halaman : '+ data[0].actionDetails[i].pageTitle+"<br>");
          document.write('URL : '+ data[0].actionDetails[i].url+"<hr>");
       }
   });
});

如果要显示data中的所有action items

$(document).ready(function() {
    $.getJSON('http://services.berthojoris.com/json/baca2.php', function(data) {

        $.each(data, function(idx, item){
            $.each(item.actionDetails, function(idx, actionDetail){
                document.write('Judul Halaman : '+ actionDetail.pageTitle+"<br>");
                document.write('URL : '+ actionDetail.url+"<hr>");
            })
        })
    });
});

另请注意,您可能必须使用 $('body').append(string) 而不是 document.write(string)

关于php - 如何读取 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18386339/

相关文章:

php - 使用 PHP MYSQL 按关键字搜索

php - 试图用 WHERE NOT EXISTS 显示两个表中的数据

javascript - .find() 不是 mongoose + nodejs 中的函数

javascript - 获取大数的最后 13 位数字

javascript - React : When not bound, 'this' 为 null,而不是 React 组件事件处理程序中未定义

jquery - Bootstrap 2.2.1 中的弹出框

在 MySQL 中插入日期时间时的 PHP date() 格式

jquery - AJAX url 在 IE 中不起作用

javascript - Keyup 不适用于动态添加的输入组

php - cloud9,无法安装 php cURL