2014-05-15 70 views
1

JSON結果如何以下使用PHP JSON輸出訪問的元素..如何閱讀從PHP

{ 
    "ISBN:9781430215752": { 
     "bib_key": "ISBN:9781430215752", 
     "preview": 
       "restricted", 
     "preview_url": 
       "https://archive.org/details/linuxrecipesforo00kuhn", 
     "info_url": "https://openlibrary.org/books/OL23936576M/Linux_recipes_for_Oracle_DBAs", 
     "details": { 
      "lc_classifications": ["QA76.9.D3 K84 2009"], 
      "latest_revision": 2, 
      "ocaid": "linuxrecipesforo00kuhn", 
      "contributions": ["Kim, Charles.", "Lopuz, Bernard."], 
      "source_records": ["marc:marc_loc_updates/v37.i44.records.utf8:10470755:1047"], 
      "title": 
        "Linux recipes for Oracle DBAs", 
      "languages": [{ 
        "key": "/languages/eng" 
       }], 
      "subjects": ["Linux", "Oracle (Computer file)", "Relational databases", "Database management"], 
      "publish_country": "cau", 
      "by_statement": "Darl Kuhn, Charles Kim, Bernard Lopuz.", 
      "type": { 
       "key": "/type/edition" 
      }, 
      "revision": 2, 
      "other_titles": ["Linux recipes for Oracle DataBase Administrators"], 
      "publishers": ["Apress", "Distributed to the book trade by Springer-Verlag"], 
      "last_modified": { 
       "type": "/type/datetime", 
       "value": "2014-04-06T06:55:36.956977" 
      }, 
      "key": "/books/OL23936576M", 
      "authors": [{ 
        "name": "Darl Kuhn", 
        "key": "/authors/OL1484587A" 
       }], 
      "publish_places": ["Berkeley, CA", "New York"], 
      "oclc_number": ["243543902"], 
      "pagination": "xxv, 501 p. :", 
      "created": { 
       "type": "/type/datetime", 
       "value": "2009-11-24T23:42:39.524606" 
      }, 
      "dewey_decimal_class": ["005.75/65 22", "005.26/8"], 
      "notes": { 
       "type": "/type/text", 
       "value": "Includes index." 
      }, 
      "number_of_pages": 501, 
      "isbn_13": ["9781430215752"], 
      "lccn": ["2009277832"], 
      "isbn_10": ["1430215755"], 
      "publish_date": "2008" 
     } 
    } 
} 

我試着用下面的代碼,
這是行不通的。

$json = json_decode($body); 

echo $json->ISBN:9780980200447->info_url; 

它給了一個錯誤..

是否有任何其他簡單的方法來讀取所有元素?

+0

嘗試的var_dump($ JSON);看看什麼是返回json_decode。 –

+0

它給了什麼錯誤?解碼的JSON是什麼樣的? (var dump或print_r it) – GordonM

回答

3

您正處在正確的軌道上。 :只是一個屬性無效的字符,必須處理特殊。

echo $json->{'ISBN:9781430215752'}->info_url; 

會給你預期的結果。

1

您還可以解碼作爲關聯數組

$json = json_decode($body, true); 
echo $json['ISBN:9781430215752']['info_url']; 

我可能會堅持這種做法,而不是面向對象的,因爲封裝鍵使代碼的可讀性

0

您必須使用

echo $json->{'ISBN:9780980200447'}->info_url; 

因爲'ISBN:9780980200447'是你的班級名稱。

參考:php.net

<?php 

$json = '{"foo-bar": 12345}'; 

$obj = json_decode($json); 
print $obj->{'foo-bar'}; // 12345 

?> 
0

使用大括號是這樣的:

echo $json->{'ISBN:9781430215752'}->info_url; 
+0

我很欣賞'大括號'和'大括號'之間的區別,但是,它們實際上被稱爲'大括號',而不是'大括號'。 http://math.about.com/od/mathhelpandtutorials/fl/Parenthesis-Braces-and-Brackets.htm – PavKR