我已经使用这个 url http://www.zachhunter.com/2010/04/json-objects-to-html-table/下载了一个示例应用程序。完美运行的部分是:
<script src="scripts/jquery-1.3.2.debug.js" type="text/javascript"></script>
<script src="scripts/json.htmTable.js" type="text/javascript"></script>
<script src="scripts/json.debug.js" type="text/javascript"></script>
<link href="styles/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function () {
/* ASSOC ARRAY - Detail View */
var json1 = { "d": [{ "__type": "acation", "id": "001", "date": "2010-06-01
00:00:00", "iod": "1", "mer": "ABC ", "tity": "6", "ot":
"12,500", "nt": "75000", "ou": "A", "rep": "we", "perc": "34",
"ine": "one", "year": "2009", "ct": "ABC ", "alet": "90000",
"pro": "1500", "stats": "ive", "crnt": "5000", "ter": "AA"}] }
/* NORMAL ARRAY - Detail View */
var json2 = { "d": [{ __type: "acation", id: "001", date: "2010-06-01
00:00:00", iod: "1", mer: "ABC ", tity: "6", ot: "12,500", nt:
"75000", ou: "A", rep: "we", perc: "34", ine: "one", year:
"2009", ct: "ABC ", alet: "90000", pro: "1500", stats: "ive",
crnt: "5000", ter: "AA"}] }
/* NORMAL ARRAY - Table View */
var json3 = { "d": "[{\"Id\":1,\"UserName\":\"Sam Smith\"},{\"Id\":2,\"UserName
\":\"Fred Frankly\"},{\"Id\":1,\"UserName\":\"Zachary
Zupers\"}]" }
$('#DynamicGridLoading').hide();
delete json1.d[0]["__type"];
delete json2.d[0]["__type"];
$('#DynamicGrid').append(CreateDetailView(json1.d, "lightPro", true)).fadeIn();
$('#DynamicGrid').append(CreateDetailView(json2.d, "lightPro", true)).fadeIn();
$('#DynamicGrid').append(CreateTableView(json3.d, "lightPro", true)).fadeIn();
});
</script>
</head>
<body>
<form id="form1" >
<div id="DynamicGrid" >
<div id="DynamicGridLoading" >
<img src="images/loading.gif" /><span> Loading Data... </span>
</div>
</div>
<br />
<a href="jsonservice_api.html">Json web service</a>
</form>
</body>
它工作正常,但是当我尝试使用像这种天气 api 这样的免费 json 网络服务时:http ://api.worldweatheronline.com/free/v1/weather.ashx?q=dehradun&format=json&num_of_days=5&key=3tkb34yntmwqn23uambzdvgm以下列方式我什么也得不到。
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://api.worldweatheronline.com/free/v1/weather.ashx?q=dehradun&
format=json&num_of_days=5&key=3tkb34yntmwqn23uambzdvgm",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{}",
success: function (res) {
$('#DynamicGrid').append(CreateTableView1(res, "CoolTableTheme",
true)).fadeIn();
}
});
});
</script>
CreateTableView 的定义位于 scripts 标记中包含的 js 文件之一,其定义如下:
function CreateTableView(objArray, theme, enableHeader) {
// set optional theme parameter
if (theme === undefined) {
theme = 'mediumTable'; //default theme
}
if (enableHeader === undefined) {
enableHeader = true; //default enable headers
}
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '<table class="' + theme + '">';
// table head
if (enableHeader) {
str += '<thead><tr>';
for (var index in array[0]) {
str += '<th scope="col">' + index + '</th>';
}
str += '</tr></thead>';
}
// table body
str += '<tbody>';
for (var i = 0; i < array.length; i++) {
str += (i % 2 == 0) ? '<tr class="alt">' : '<tr>';
for (var index in array[i]) {
str += '<td>' + array[i][index] + '</td>';
}
str += '</tr>';
}
str += '</tbody>'
str += '</table>';
return str;
}