jquery - 如何获取数据json
问题描述
我想用 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
谢谢你的帮助!解决方案
我想看看你是怎么设置的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
推荐阅读
- c++ - 在哪里可以找到 Clang-tidy“可读性标识符命名”检查器的可用选项列表?
- laravel - 如何在 vuejs 组件中显示嵌套数组数据
- css - CSS - 具有右边距的 Flex 元素
- sql - 将一个变量中的不同值组合在一起
- html - Childs div大小与最大的div css相同
- javascript - 如何从 C# 创建的 JavaScript 中的数据表中检查字段的值?
- ios - Swift 4.2 - Cocoapods:在 resource_bundles 中添加和访问 scnassets
- java - 当我通过 java 代码运行 jmeter 时出现错误 converters.ConversionException
- java - Java 中的 System.in.read() 阻塞多长时间
- mysql - 如何使这个 MySQL 查询(带有子查询)更快?