2012-10-08 51 views
0
{ 
    "kind": "books#volume", 
    "id": "a3ERAAAAYAAJ", 
    "etag": "Pax/JBMS5hw", 
    "selfLink": "https://www.googleapis.com/books/v1/volumes/a3ERAAAAYAAJ", 
    "volumeInfo": { 
    "title": "Passion-flowers", 
    "authors": [ 
    "Julia Ward Howe" 
    ], 
    "publishedDate": "1854", 
    "industryIdentifiers": [ 
    { 
     "type": "OTHER", 
     "identifier": "HARVARD:32044023800626" 
    } 
    ], 
    "pageCount": 187, 
    "printType": "BOOK", 
    "contentVersion": "full-1.0.0", 
    "imageLinks": { 
    "smallThumbnail": "http://bks2.books.google.it/books?id=a3ERAAAAYAAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api", 
    "thumbnail": "http://bks2.books.google.it/books?id=a3ERAAAAYAAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" 
    }, 
    "language": "en", 
    "previewLink": "http://books.google.it/books?id=a3ERAAAAYAAJ&printsec=frontcover&dq=flowers&as_brr=7&hl=&cd=1&source=gbs_api", 
    "infoLink": "http://books.google.it/books?id=a3ERAAAAYAAJ&dq=flowers&as_brr=7&hl=&source=gbs_api", 
    "canonicalVolumeLink": "http://books.google.it/books/about/Passion_flowers.html?hl=&id=a3ERAAAAYAAJ" 
    }, 
    "saleInfo": { 
    "country": "IT", 
    "saleability": "FREE", 
    "isEbook": true 
    }, 
    "accessInfo": { 
    "country": "IT", 
    "viewability": "ALL_PAGES", 
    "embeddable": true, 
    "publicDomain": true, 
    "textToSpeechPermission": "ALLOWED", 
    "epub": { 
    "isAvailable": false, 
    "downloadLink": "http://books.google.it/books/download/Passion_flowers.epub?id=a3ERAAAAYAAJ&hl=&output=epub&source=gbs_api" 
    }, 
    "pdf": { 
    "isAvailable": true, 
    "downloadLink": "http://books.google.it/books/download/Passion_flowers.pdf?id=a3ERAAAAYAAJ&hl=&output=pdf&sig=ACfU3U0sPdmPZp_LmFzZXatBjMeV54xJxA&source=gbs_api" 
    }, 
    "webReaderLink": "http://books.google.it/books/reader?id=a3ERAAAAYAAJ&as_brr=7&hl=&printsec=frontcover&output=reader&source=gbs_api", 
    "accessViewStatus": "FULL_PUBLIC_DOMAIN" 
    } 
    } 

你好,我有一個Json文件,看起來像我上面張貼的一塊。 我想提取該文件中包含的所有標題以及縮略圖網址。我試着用此代碼訪問的第一個冠軍,但因爲它沒有工作:如何使用php json_decode()從JSON文件中提取精確值?

<?php 

$file = file_get_contents("volumes.json"); 

$json = json_decode($file, true); 

$json->items->volumeInfo[0]->title; 

?> 

我得到試圖讓非對象的屬性在第7行爲什麼我不能訪問$ json作爲一個對象?我能做些什麼來提取所有的數據?由於

+0

您忘記在數據中顯示「項目」了嗎? –

+0

如果您厭倦了' - >''''''''''''''''''''可以嘗試將json轉換爲具有第二個'true'參數給json_decode()'的數組。 – complex857

回答

1

您可以嘗試

$json = json_decode($file, true); 
var_dump($json['volumeInfo']['title']); 

或者

$json = json_decode($file); 
var_dump($json->volumeInfo->title); 

輸出

string 'Passion-flowers' (length=15) 
+0

他們都沒有工作... – Haldir87

+0

@Eros我總是測試我的代碼看現場演示:http://codepad.viper-7.com/LOTwuL – Baba

+0

@Eros在另一臺服務器上看到它http://codepad.org/ xOjSj3eZ – Baba

0

如果你明確地告訴json_decode()總是返回數組(第二個參數,這是很好的),你不應該嘗試使用對象表示法訪問結果中的任何內容。