如何读取json数组? [英] How to read json arrays?

查看:126
本文介绍了如何读取json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取此json数组?我得到不确定的结果.我在做什么错了?

How can I read this json array? I am getting undefined results. What am I doing wrong?

= jSON文件==

= jSON file ==

// 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,因为它不是数组.我想你想要的是

You can't run each on data as it is not an array. I think what you want is

$.each(data.items, function(entryIndex, entry) {

这篇关于如何读取json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆