繁体   English   中英

JSON不显示数据

[英]JSON doesn't show data

我正在尝试学习如何使用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"
                }
            ]
        }
    ]
}

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

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

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

它应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM