从json_encode访问数据

时间:2011-06-03 03:29:36

标签: javascript ajax json

以下是json_encode()在ajax页面生成的代码。现在,我想检索数据(标题,ISBN,作者......)。任何人都可以帮我一把吗?我查看了jQuery.parseJSON函数,但感到困惑。

{
    "9780077225957": {
        "Items_Data": {
            "Title": "Developing Management Skills: What Great Managers Know and Do",
            "Data_Source": "Amazon",
            "Item_ID": "1329",
            "ISBN": "9780077225957",
            "Authors": "Timothy Baldwin",
            "Edition": "1",
            "Year": "2007",
            "Publisher": "McGraw-Hill/Irwin",
            "Amazon_Thumb_URL": "http://ecx.images-amazon.com/images/I/41pVf7GKujL._SL160_.jpg"
        }
    }
}

2 个答案:

答案 0 :(得分:0)

只需在JavaScript变量中进行分配即可,但我认为您需要首先将9780077225957更改为键,因为您不会使用(。)运算符访问它。我已将其更改为Book9780077225957并正常工作。这是我用过的代码

var book = { "Book9780077225957" : { "Items_Data": { "Title": "Developing Management Skills: What Great Managers Know and Do", "Data_Source": "Amazon", "Item_ID": "1329", "ISBN": "9780077225957", "Authors": "Timothy Baldwin", "Edition": "1", "Year": "2007", "Publisher": "McGraw-Hill/Irwin", "Amazon_Thumb_URL": "http://ecx.images-amazon.com/images/I/41pVf7GKujL.SL160.jpg" } } };

alert(book.Book9780077225957.Items_Data.Title);

答案 1 :(得分:0)

我希望做类似的事情:

var Books = {
    toElement: function(data) {
        if (data) {

            $book = $('<div></div>')
                .addClass('book');

            $('<span></span>').className("title").text(data.Title)appendTo($book);
            $('<span></span>').className("authors").text(data.Authors)appendTo($book);
            $('<span></span>').className("isbn").text(data.ISBN)appendTo($book);

            $book.appendTo($('#books'));            
        }
    }
};

$(function() {
     $("#Button1").click(function() {
        var params = [];
        $.getJSON("book_url.js", params, function(returnedJSON) {
            Books.toElement(returnedJSON.Items_Data);           
        });

     });
});

我不确定JSON中的第一个元素是什么。它可能不会返回JSON.Items_Data。出于某种原因,我认为第一级被忽略。但我可能会考虑其他事情。

希望这有帮助。