【发布时间】:2015-01-15 16:03:55
【问题描述】:
我正在尝试将以下 JSON 提要解析为 HTML,但没有得到任何结果。
{
@name: "App name",
license: [
{
@name: "Apache License 2.0",
component: [
{
name: "Product 1",
url: "http://www.url.com"
}, {
name: "Product 2",
url: "http://www.url.com"
}, {
name: "Product 3",
url: "http://www.url.com"
}
],
licensetext: " license text here"
}
],
isProjectComplete: "true"
}
使用这个 jQuery:
$.getJSON("https://json-feed-url.json", function (data) {
var jsondata = json;
var output = "<ul>";
for (var i in jsondata.events) {
output += "<li>"
+ jsondata.component[i].name + " "
+ jsondata.component[i].url + "--"
+ jsondata.license[i].licensetext
+ "</li>";
}
output += "</ul>";
document.getElementById("content").innerHTML = output;
});
到这个div
<div id="content"></div>
不知道为什么它没有正确解析。这是jsFiddle。
【问题讨论】:
-
不确定您的 JSON 是否有效,请验证 here。有效的 JSON 应以
{"@name": "App name"开头,另一方面,对于跨域请求,您需要 JSONp。 -
您的密钥需要被引用:例如
"@name"代替@name,"license"代替license等 -
@skobaljic,JSON 有效,并且直接来自 AWS 实例。我会分享 URL,但它有一个查看提要所需的密钥。