使用jQuery显示JSON中的数据 [英] Show data from JSON using jQuery

查看:72
本文介绍了使用jQuery显示JSON中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON:

var data = [{"datapresub":"08\/08\/2018","datasub":"08\/08\/2018","nomeComune":"ROMA","provincia":"RM"}]

我想使用jQuery显示Json的每个值的结果.问题在于该脚本仅显示索引为零的对象的结果.

I want to show the results for each value of the Json using jQuery. The problem is that this script show the results only for the object that have index zero.

有解决方案吗?

我尝试过:

function(data) {
                            var jsonData = JSON.parse(data),
                                filterData = jsonData,
                                i;
                            for (i = 0; i < filterData.length; i++ ) {
                                if(filterData[i]['datasub']) {
                                    $('.nome-comune').html(filterData[i]['nomeComune']);
                                    $('.provincia').html(filterData[i]['provincia']);
                                    $('.data-sub').html(filterData[i]['datasub']);
                                    resultsSub();
                                } else if(filterData[i]['datapresub'] && filterData[i]['datasub'] == 0) {
                                    $('.nome-comune').html(filterData[i]['nomeComune']);
                                    $('.provincia').html(filterData[i]['provincia']);
                                    $('.data-presub').html(filterData[i]['datapresub']);
                                    resultsPresub();
                                }

                            }


                        }

Json数据是通过PHP脚本传递的,该脚本可过滤来自API的数据.

the Json data is passed from a PHP script that filter data coming from an API.

谢谢

推荐答案

数据变量是否具有更多数组?你算filterData.length. 有数据显示在表html上,使用append.

are data variable have more array? you count filterData.length. There are data show at table html use append.

var data = [{"datapresub":"08\/08\/2018","datasub":"08\/08\/2018","nomeComune":"ROMA","provincia":"RM"}];
    console.log(data);

    let filterData = data;
    let tr = '';
    for (i = 0; i < filterData.length; i++ ) {
      tr += '<tr>'+
          '<td>'+filterData[i].datapresub+'</td>'+
          '<td>'+filterData[i].datasub+'</td>'+
          '<td>'+filterData[i].nomeComune+'</td>'+
          '<td>'+filterData[i].provincia+'</td>'+
        '</tr>';       
    }
    $('#table_element').append(tr);

,然后在视图页面中添加表格元素.

and add table element at your view page.

<table id="table_element" border=1></table>

这篇关于使用jQuery显示JSON中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆