【问题标题】:How to get data json如何获取数据json
【发布时间】:2021-05-16 07:59:57
【问题描述】:

我想用 JQUERY 获取 Json 数据,但我的变量未定义。


        var itech_centre='';
      
    $.each(data, function(key,value){
    
        itech_centre+='<tr>';
        itech_centre+='<td>'+value.c_com_nom+'</td>';
        itech_centre+='<td>'+value.c_adr_voie+'</br>'+value.c_com_cp+'</br>'+value.c_com_nom+'</td>';
        itech_centre+='<td>'+value.c_rdv_tel+'</td>';
        
        itech_centre+='</tr>';
    });
    
    $('#centreVaccination').append(itech_centre);
   
    
    });

这是我的 HTML

 <table class="table table-bordered table-striped" id="centreVaccination">
                            <tr>
                                <th>Centre</th>
                                <th>Adresse</th>
                                <th>Tel</th>
                                
                            </tr>

还有我的 JSON 链接:https://www.data.gouv.fr/fr/datasets/r/d0566522-604d-4af6-be44-a26eefa01756

谢谢你的帮助!

【问题讨论】:

  • 我没有看到任何加载 JSON 的尝试。

标签: jquery json getjson


【解决方案1】:

我想看看你如何设置data。仅将该链接中的 json 设置为 data 将不起作用,因为您尝试获取的值(c_com_nom 等)嵌套在 features 数组下方和 properties 对象内。

我会通过创建一个 var 来保存 features 数组,然后循环遍历每个功能并从属性对象中获取您想要的属性。

示例(略带伪代码):

// Import json here
const features = data.features;
let rows = "";
features.forEach(feature => {
  let row = "<tr>";
  row += "<td>" + feature.properties.c_com_nom + "</td>"
  // ...
  row += "</tr>";
  rows += row;
}
// Append rows to your table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2021-12-08
    • 1970-01-01
    • 2016-06-15
    • 2014-03-13
    相关资源
    最近更新 更多