简体   繁体   中英

How can I extract exact values from a JSON file with php json_decode()?

    {
   "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"
   }
  }

Hello, I've got a Json file that looks like the piece that I posted above. I would like to extract all the titles contained into this file as well as the thumbnail urls. I tried to access the first title item with this code, but it didn't work out:

<?php

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

$json = json_decode($file, true);

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

?>

I get Trying to get property of non-object in line 7. Why can't I access the $json as an object? And what can I do to extract all the data? Thanks

You can try

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

Or

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

Output

string 'Passion-flowers' (length=15)

On issue is that you're loading $json as an array. If you want to load it as an object, you'll want to do:

$json = json_decode($file, false);

http://php.net/manual/en/function.json-decode.php

如果您明确地告诉json_decode()始终返回数组(第二个参数,这很好),则不应尝试使用对象表示法访问结果内部的任何内容。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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