2011-10-25 188 views
2

我有以下的JSON內容我是從一個服務進拉:JSON解析與PHP

[ 
    { 
     "global_event":{ 
     "ending_at":"2011-11-07T02:00:00Z", 
     "short_url":"http://bit.ly/reAhRw", 
     "created_at":"2011-10-04T14:25:41Z", 
     "event_responses":[ 

     ], 
     "addresses":{ 
      "location":{ 
       "city":"blah", 
       "latitude":30.205288, 
       "zipcode":"343434", 
       "street":"blah", 
       "longitude":-95.475289, 
       "state":"TX" 
      } 
     }, 
     "body":"blahblahblah", 
     "euid":"2f489d0c82d167f1c16aba5d3b4c29ade6f1d52a", 
     "title":"Fusion", 
     "updated_at":"2011-10-04T14:26:57Z", 
     "event_roles":[ 

     ], 
     "user":{ 
      "long_name":"Fusion Single", 
      "nickname":"" 
     }, 
     "event_items":[ 

     ], 
     "starting_at":"2011-11-07T00:00:00Z" 
     } 
    } 
] 

我試過下面的代碼來解析它無濟於事:

$json = @file_get_contents('jsonfeed'); 
$feed = json_decode($json); 

foreach($feed->global_event as $item) { 
      $rss_item = array(
       'title' => $item->title, 
       'link' => $item->short_url, 
       'author' => $item->long_name, 
       'content' => $item->body, 
       'date' => $item->updated_at, 
       'type' => 'Woodlands Church' 
      ); 
      array_push($this->rss, $rss_item); 
     } 

創建的最終數組$this->rss從來沒有任何內容,只是一個空數組。有任何想法嗎?

+4

您foreaching這個數組是錯誤的。您的字段不在JSON數組中的'$ feed-> global_event'下。 – OptimusCrime

回答

4

在JSON中,花括號(「{」和「}」)定義對象,而不是數組。尖括號定義數組。

so $feed是一個數組,其中包含1個對象,其中1個屬性名爲global_event

循環應該是:

$feed = json_decode($json); 
foreach($feed as $obj) { 
    $item = $obj->global_event; 

    $rss_item = array(
     'title' => $item->title, 
     'link' => $item->short_url, 
     'author' => $item->long_name, 
     'content' => $item->body, 
     'date' => $item->updated_at, 
     'type' => 'Woodlands Church' 
    ); 
    array_push($this->rss, $rss_item); 
} 
+0

非常感謝您的幫助! – John

2

你需要這樣分析它:

<?php 

$json = @file_get_contents("jsonfeed"); 
$feed = json_decode($json); 

foreach($feed as $item) { 
    // your code, accessing everything by using 
    // $item->global_event->PROPERTY 
} 

?> 

因爲你foreach循環的開始你的$feed變量是這樣的:

Array 
(
[0] => stdClass Object 
    (
     [global_event] => stdClass Object 
      (
       [ending_at] => 2011-11-07T02:00:00Z 
       [short_url] => http://bit.ly/reAhRw 
       [created_at] => 2011-10-04T14:25:41Z 
       [event_responses] => Array 
        (
        ) 

       [addresses] => stdClass Object 
        (
         [location] => stdClass Object 
          (
           [city] => blah 
           [latitude] => 30.205288 
           [zipcode] => 343434 
           [street] => blah 
           [longitude] => -95.475289 
           [state] => TX 
          ) 

        ) 

       [body] => blahblahblah 
       [euid] => 2f489d0c82d167f1c16aba5d3b4c29ade6f1d52a 
       [title] => Fusion 
       [updated_at] => 2011-10-04T14:26:57Z 
       [event_roles] => Array 
        (
        ) 

       [user] => stdClass Object 
        (
         [long_name] => Fusion Single 
         [nickname] => 
        ) 

       [event_items] => Array 
        (
        ) 

       [starting_at] => 2011-11-07T00:00:00Z 
      ) 

    ) 

) 

請務必注意什麼是一個對象以及什麼是數組,以便使用適當的方法來訪問數據(所以,帶有 - >符號的對象和帶有[]符號的數組)。