如何访问json中的数组元素值
问题描述:
我无法访问我的html页面中的_sValues,但是我可以在控制台中console.log感兴趣的_sValues数组元素。如何访问json中的数组元素值
JSON
[
{
"_this":{
"_isEvent":false,
"_sNames":[
"name",
"tag",
"notes",
"input",
"type",
"action"
],
"_sValues":[
"Testing01",
"#13",
"1504013826",
"No details",
"cType",
"NA"
],
"_cName":"namesList",
"_dName":"TEST",
"_id":"12345",
}
}
]
HTML
<th class="col-md-5 col-xs-5" data-field="_this._sValues[1]" data-sortable="true">S-VALUES</th>
我想看看#13显示在页面上,而不是我没有看到任何值或控制台错误。但是我可以通过做_this.sValues
SCRIPT
var data;
$(function() {
$.getJSON("http://localhost:8080/services/webapi/getAllData", function(json){
data = json;
$('#table').bootstrapTable({
data: data
});
});
});
DEV TOOL
data[0]._this._sValues[1]
"#13"
答
在你的情况, 显示所有值我假设你只有一排。
$(".col-md-5").attr("data-field",data[0]._this._sValues[1]);
它会将数据注入到html中。 如果你有多行,你可以使用for循环。
答
我相信你有一个沉默的错误。它与数据对象中的尾随逗号有关。
"_id":"12345",
// should be
"_id":"12345"
这就是我所注意到的。库尔贝错了,但我知道拖尾的逗号与一些浏览器混淆。
你是如何将你的javascript值注入到html中的? – quickshiftin
@quickshiftin用脚本功能更新了问题 –