2011-06-07 67 views
2

我有一個PHP腳本成功地使用JSON字符串解碼爲PHP對象:PHP訪問解碼JSON

$decodedJSON = json_decode($responseString); 

$decodedJSON被打印到響應,我得到:

stdClass Object 
(
    [response] => stdClass Object 
     (
      [total_rows] => 2379 
      [data] => Array 
       (
        [0] => Array 
         (
          [0] => zPKfssdfg456hwCQp6XpogfTsjc 
          [1] => 3456gg4-b7ea-4bcf1680c619 
          [2] => Title Description Here. 
          [3] => Example Address 
          [4] => 
          [5] => 
          [6] => Exmaple City 
          [7] => CA 
          [8] => US 
          [9] => 95616 
          [10] => (555) 555-1212 
          [11] => 
          [12] => Food & Beverage 
          [13] => 
          [14] => 
          [15] => 62.79901 
          [16] => -92.80102 
          [17] => 1 
         ) 

        [1] => Array 
         (
          [0] => zPKfl6ylrwCQp6XpogfTsjc 
          [1] => 98e9945a-b7ea-4bcf1680c619 
          [2] => Title Description Here. 
          [3] => Example Address 
          [4] => 
          [5] => 
          [6] => Exmaple City 
          [7] => CA 
          [8] => US 
          [9] => 95616 
          [10] => (555) 555-1212 
          [11] => 
          [12] => Food & Beverage 
          [13] => 
          [14] => 
          [15] => 62.79901 
          [16] => -92.80102 
          [17] => 1 
         ) 

       ) 

      [fields] => Array 
       (
        [0] => subject_key 
        [1] => factual_id 
        [2] => name 
        [3] => address 
        [4] => address_extended 
        [5] => po_box 
        [6] => locality 
        [7] => region 
        [8] => country 
        [9] => postcode 
        [10] => tel 
        [11] => fax 
        [12] => category 
        [13] => website 
        [14] => email 
        [15] => latitude 
        [16] => longitude 
        [17] => status 
       ) 

      [rows] => 10 
      [cache-state] => CACHED 
      [big-data] => 1 
      [subject_columns] => Array 
       (
        [0] => 1 
       ) 

     ) 

    [version] => 2 
    [status] => ok 
) 

我的問題是:我如何訪問「字段」數據?我試過使用$decodedJSON["fields"]以及$decodedJSON[0]["fields"]$decodedJSON[0][2]以及許多更多的變體。

有沒有人看到我做錯了什麼?

非常感謝, 佈雷特

回答

6

使用$decodedJSON->response->fields

+0

完美!非常感謝;-)只要SO允許,我會立即接受答案 – Brett 2011-06-07 20:17:56

+3

如果查看輸出結果,您會看到'$ decodedJSON'和'response'屬性都是對象,這就是爲什麼您需要用箭頭符號' - >'來訪問它們。 – jokkedk 2011-06-07 20:23:30

5

那麼你可以將其解碼爲關聯數組嘗試:
$decodedJSON = json_decode($responseString, true);
然後你就可以訪問你試圖

OR: $decodedJSON->response->fields

+0

Upvote用於解釋如何以OP的方式訪問它,以及PHP如何訪問 – Martin 2012-04-16 13:50:03