JSON没有显示数据

时间:2014-05-30 13:23:59

标签: jquery json

我正在尝试学习如何使用JSON,但我已经陷入了基础...... 现在我想在我的ID Content中显示来自json文件的数据:author。

但是当我点击元素时它似乎不起作用。

脚本:

$(document).ready( function()
    {
        $('a').click(function(event)
        {
            event.preventDefault();

            $.ajax(
            {
                url:        'Booklist.json',
                dataType:   'json',
                success:    function(data)
                {   

                    $.each(data.Booklist, function(index, item)
                    {
                        $('#content').append(data.Booklist[0].author);
                        console.log(item);
                    });

                },
                error: function()
                {
                    alert("Houston, we have a problem");
                }   
            });             
        });
    });

JSON:

{
    "books": [
        {
            "title": "HTML5 Media, Integrating audio and video with the Web",
            "author": "Shelley Powers",
            "desc": "A detailed introduction to presenting audio and video in HTML5, from markup through scripting. It will explain not just placing content in pages but interaction through Javascript APIs, to build media players that could be used cross-browser.",
            "publishDate": "July 2011",
            "url": "http://oreilly.com/catalog/0636920019824/",
            "cover": [
                {
                    "type": "bigCover",
                    "source": "img/cat_005.gif",
                    "alternative": "HTML5 Media"
                },
                {
                    "type": "cover",
                    "source": "img/bkt_005.gif"
                }
            ]
        },
        {
            "title": "HTML5: Up and Running",
            "author": "Mark Pilgrim",
            "desc": "If you don't know about the new features available in HTML5, now's the time to find out. This book provides practical information about how and why the latest version of this markup language will significantly change the way you develop for the Web. HTML5: Up & Running carefully guides you though the important changes in this version with lots of hands-on examples, including markup, graphics, and screenshots. You'll learn how to use HTML5 markup to add video, offline capabilities, and more -- and youÕll be able to put that functionality to work right away.",
            "publishDate": "August 2010",
            "url": "http://oreilly.com/catalog/9780596806033/",
            "cover": [
                {
                    "type": "bigCover",
                    "source": "img/cat_noCover.gif",
                    "alternative": "HTML5 Up and Running"
                },
                {
                    "type": "cover",
                    "source": "img/bkt_noCover.gif"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

JSON中的数组名为books,而不是Booklist,因此请更改此行:

$.each(data.Booklist, function(index, item)

$.each(data.books, function(index, item)

它应该有用。