如何读取json数组?
我该如何阅读这个json数组?我得到未定义的结果。我究竟做错了什么?如何读取json数组?
= JSON文件==
// JSON
{
"items": [
{
"title": "welcoem home",
"author": "Charles Dickens"
},
{
"title": "Harry Potter",
"author": "J rowling"
}]
}
<script language="javascript" >
$(document).ready(function() {
$('#letter-a a').click(function() {
$.getJSON('q3.json', function(data) {
$('#dictionary').empty();
$.each(data, function(entryIndex, entry) {
var html = '<div class="entry">';
html += '<h3 class="title">' + entry['title'] + '</h3>';
html += '<div class="author">' + entry['author'] + '</div>';
html += '<div class="definition">';
if (entry['items']) {
html += '<div class="quote">';
$.each(entry['items'], function(lineIndex, line) {
html += '<div class="quote-line">' + line + '</div>';
});
if (entry['author']) {
html += '<div class="quote-author">' + entry['author'] + '</div>';
}
html += '</div>';
}
html += '</div>';
html += '</div>';
$('#dictionary').append(html);
});
});
return false;
});
});
</script>
<link rel="stylesheet" href="json.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="header">
<h2>json stuff</h2>
</div>
<div class="letters">
<div class="letter" id="letter-a">
<h3><a href="#">A</a></h3>
</div>
</div>
<div id="dictionary">
</div>
</div>
$.each(data, function(entryIndex, entry) {
不能在data
运行each
,因为它不是一个数组。我想你想要的是
$.each(data.items, function(entryIndex, entry) {
谢谢。那工作。是否有任何其他的方式不使用jquery? - 感谢 – user244394 2010-10-26 01:29:28
您可以使用几乎任何其他库/框架,或者用纯JavaScript书写所有代码。 – Phil 2010-10-26 02:05:26
你会如何阅读这个使用普通的JavaScript? – user244394 2010-10-28 23:31:09
错误是什么线?虽然在这里我们可以弄明白,为什么让我们猜测? – 2010-10-26 00:56:44