我有以下的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
從來沒有任何內容,只是一個空數組。有任何想法嗎?
您foreaching這個數組是錯誤的。您的字段不在JSON數組中的'$ feed-> global_event'下。 – OptimusCrime