weixin_33688840 2017-11-10 01:27 采纳率: 0%
浏览 60

将Json导入html(表)

im trying to import a json url to html in a table. The problem is when i get the data i get 25 rows i get this on the web console: Object { data: Array[25], paging: Object } I also have the following code which is designed for only one row i guess .And i understand i have Loop over each object, appending a table row with the relevant data of each iteration. The problem is i don´t how to do it, i´m not an expert . Thank you for your help!

This is the data i get on the json url :

 {
   "data": [
      {
         "created_time": "2017-11-10T01:24:47+0000",
         "permalink_url": "https://www.facebook.com/DBZSFANSOFICIAL2/posts/1539014319521507",
         "id": "949007375188874_1539014319521507"
      },
      {
         "created_time": "2017-11-10T01:23:37+0000",
         "permalink_url": "https://www.facebook.com/DBZSFANSOFICIAL2/posts/1539013649521574",
         "id": "949007375188874_1539013649521574"
      },
      {
         "created_time": "2017-11-09T23:59:15+0000",
         "permalink_url": "https://www.facebook.com/DBZSFANSOFICIAL2/posts/1538951229527816",
         "id": "949007375188874_1538951229527816",
         "shares": {
            "count": 20
         }
      },
      {
         "created_time": "2017-11-09T23:32:30+0000",
         "permalink_url": "https://www.facebook.com/DBZSFANSOFICIAL2/posts/1538935439529395",
         "id": "949007375188874_1538935439529395"
      },

And this my code

<body>
    <input type="text" class="txtPagina">
    <button class="btnBuscar">Buscar</button>
    <table class="tabla" border='1'>
         <tr>

             <td>created time</td>
             <td>permalink url</td>
             <td>id</td>
             <td>Shares Count</td>


         </tr>
    </table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $('.btnBuscar').on('click', function (){
            var pagina = $('.txtPagina').val();

            //Ajax
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "https://graph.facebook.com/"+pagina+"/feed?fields=created_time,permalink_url,id,shares& access_token=mytoken",
                success: function(data){
                    console.log(data);
                   $('.tabla').append("<tr><td>"+data.created_time+"</td><td>"+data.permalink_url+"</td><td>"+data.id+"</td><td>"+data.shares+"</td></tr>");
                },
                error: function (){
                    console.log("Error");
                }
            });
        });
    });
</script> 
</body>
</html>
  • 写回答

2条回答 默认 最新

  • weixin_33739541 2017-11-10 01:37
    关注
    success: function(data){
       console.log(data);
      $('.tabla').append("<tr><td>"+data.created_time+"</td><td>"+data.permalink_url+"</td><td>"+data.id+"</td><td>"+data.shares+"</td></tr>");
     },
    

    should be

    success: function(data){
      $.each(data.data, function(i, d){
        var s = d.shares ? '<td>'+d.shares.count+'</td>' : '';
        $('.tabla').append('<tr><td>'+d.created_time+'</td><td>'+d.permalink_url+'</td><td>'+d.id+'</td>'+s+'</tr>');
      });
    },
    
    评论

报告相同问题?

悬赏问题

  • ¥15 readimage函数怎么读取变量图片地址
  • ¥50 网课里面实习定位打卡
  • ¥50 Delphi 非客户区窗口阴影?
  • ¥15 cv2 morphologyEx函数报错
  • ¥15 有没有知道鸿蒙OS高级开发者新题答案的
  • ¥15 有没有人能帮我一下android
  • ¥20 做一个干部信息管理系统 软件
  • ¥15 通过4G模块EC600N向阿里云物联网平台物模型上面发送字符串,现在发送int数据是成功的,发送字符串就是不成功
  • ¥15 IDA反编译,代码识别失败
  • ¥70 matlab代码修改