JSON解析与PHP [英] JSON Parsing with PHP

查看:181
本文介绍了JSON解析与PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务Feed中提取了以下JSON内容:

  [
{
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 ,
longitude: - 95.475289,
state:TX
}
},
body:blahblahblah,
euid:2f489d0c82d167f1c16aba5d3b4c29ade6f1d52a,
title:Fusion,
updated_at:2011-10-04T14:26:57Z,
event_roles:[

],
用户:{
long_name:Fusion Single,
昵称:

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);



$ b

创建的最终数组 $ this-> ; rss 从来没有任何东西,只是一个空数组。任何想法?在bson中,花括号({和})定义对象,而不是数组。尖括号定义数组。

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);
}


I have the following JSON content that I'm pulling from a service feed:

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

I've tried the following code to parse it to no avail:

$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);  
        }

The final array that is created $this->rss never has anything in it and is just a null array. Any ideas?

解决方案

In JSON, curly brackets ("{" and "}") define objects, not arrays. Angle brackets define arrays.

so $feed is an array, containing 1 object with 1 property called global_event.

the loop should be:

$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);  
}

这篇关于JSON解析与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆