使用json访问数组中的元素

时间:2014-06-24 08:36:43

标签: javascript php json

我有以下php代码:

<?php
$library =
    '{
        "closets":[
                    {"id":"001","theme":"literature","shelves":
                    {
                        "books":[
                            {"id":"b1","genre":"english Literature"},
                            {"id":"b2","genre":"arabic literature"},
                            {"id":"b3","genre":"french literature"}  
                        ]
                    }
                    },
                    {"id":"002","theme":"comedy","shelves":{
                            "books":[
                                {"id":"b11","genre":"english comedy"},
                                {"id":"b22","genre":"arabic comedy"},
                                {"id":"b33","genre":"french comedy"}  
                            ]
                        }
                    },
                    {"id":"003","theme":"history","shelves":{
                            "books":[
                                {"id":"b11","genre":"english history"},
                                {"id":"b22","genre":"arabic history"},
                                {"id":"b33","genre":"french history"}  
                            ]
                        }
                    }
        ]
    }';

$literature = $_POST["literature"];
$comedy     = $_POST["comedy"];
$history    = $_POST["history"];

$literatureBooks = library.closets.shelves.books;
$comedyBooks     = library.closets.shelves.books;
$historyBooks    = library.closets.shelves.books;

if ($literature) {
    echo $literatureBooks;
} elseif ($comedy) {
    echo $comedyBooks;
} elseif ($history) {
    echo $historyBooks;
}
?>

以及以下javascript:     $(文件)。就绪(函数(){     $( “#文献”)。单击(函数(){

$.ajax({
    type:'post';
    url:'library.php';
    data:'literature';
    dataType:'json';
    success: function(data)
    {
        for (var i in data){
            $("#books").append('literature Books are'+data[i]+<'br/'>);
        }
    }


});
}); 
$("#comedy").click(function(){

$.ajax({
    type:'post';
    url:'library.php';
    data:'comedy';
    dataType:'json';
    success: function(data)
    {
        for (var i in data){
            $("#books").append('comedy Books are'+data[i]+<'br/'>);
        }
    }


});
}); 
$("#history").click(function(){

$.ajax({
    type:'post';
    url:'library.php';
    data:'history';
    dataType:'json';
    success: function(data)
    {
        for (var i in data){
            $("#books").append('history Books are'+data[i]+<'br/'>);
        }
    }


});
}); 
});
当你点击一个例如文献按钮时,html代码是一个三个按钮,所有书籍ID应该被打印(b1,b2,b3)。我已经编写了代码,但我不知道如何访问该元素预订数组库。  所以我的问题在于宣布这些变量:

$literatureBooks=library.closets.shelves.books;
$comedyBooks=library.closets.shelves.books;
$historyBooks=library.closets.shelves.books;

1 个答案:

答案 0 :(得分:0)

要使用PHP处理JSON,必须将其转换为PHP数据结构。之后,您可以访问所需的任何内容:

$library = json_decode($library, true);
$literatureBooks = $library['closets'][0]['shelves']['books'];
$comedyBooks     = $library['closets'][1]['shelves']['books'];
$historyBooks    = $library['closets'][2]['shelves']['books'];

您也可以将$ library转换为对象,而不是数组:

$library = json_decode($library);
$literatureBooks = $library->closets[0]->shelves->books;
$comedyBooks     = $library->closets[1]->shelves->books;
$historyBooks    = $library->closets[2]->shelves->books;

对于ajax调用,请使用以下内容:

$.ajax({
    type:'post',        // <----- Use comas here, not semicolon
    url:'library.php',
    data:'closet=literature',
    // ...

然后在PHP中,您可以跳过整个if ($literature) {}块,然后回显正确的壁橱:

$closet = $_POST['closet']; # You should sanitize this
echo $($closet . 'Books');