簡體   English   中英

使用json訪問數組中的元素

[英]accessing an element in the array using 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:$(document).ready(function(){$(“#literature”)。click(function(){

$.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;

要使用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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM