我在下面的代码中有以下代码,我试图从json文件中获取大写,城市等的嵌套值,并为大写,城市等创建单独的表。
我尝试了以下代码,但我没有得到任何结果。
HTML
<table id="table">
<thead>
<tr>
<th>Head 1</th>
<th>Head 1</th>
<th>Head 1</th>
<th>Head 1</th>
<th>Head 1</th>
</tr>
</thead>
<tbody id="tablebody"></tbody>
</table>
JS
<script language="javascript">
window.getContent = function(URL) {
//////////////
var jsonData =URL;
console.log(" jsonData " + jsonData );
$.ajax({
url: jsonData ,
type: "get",
dataType: "json",
//jsonp:"jsoncallback",
success: function(data, textStatus, jqXHR) {
$('#records_table > tbody').empty();
var trHTML = '';
console.log("data " +data);
$.each(data.capital, function (i, item) {
console.log(" i " + i + " item+ "+item);
console.log(" i " + i + " item+ value"+item.capital);
console.log(" i " + i + " item+ value"+this.metricShortName);
console.log(" i " + i + " item+ value"+item.value);
/***************************/
$.each(item.capital, function (k, v) {
console.log("======================");
console.log(" i " + k + " V " +v);
console.log(" i " + k + " item+ value"+v.value);
console.log(" i " + k + " item+ value"+v.name);
/**
trHTML += '<tr>' +
'<td>' + item.name + '</td>' +
'<td>' + item.value + '</td>' +
'<td>' + item.description + '</td>' +
'<td>' + item.targetValue + '</td>' +
'</tr>';
**/
/***********************************/
});
/**************************/
});
$('#table > tbody').append(trHTML);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('FAILED to get JSON from AJAX call' + jqXHR + textStatus + errorThrown);
}
});
//////////////////
}
//table cell selection
$(document).ready(function(){
var url ="metric.json";
console.log(url);
getContent(url);
});
</script>
答案 0 :(得分:1)
其data.capital.List
,List属性是一个数组
$.each(data.capital.List, function (i, item) {
trHTML += '<tr>' +
'<td>' + item.name + '</td>' +
'<td>' + item.value + '</td>' +
'<td>' + item.description + '</td>' +
'<td>' + item.targetValue + '</td>' +
'</tr>';
});