【问题标题】:JSON Parsing with PHP使用 PHP 解析 JSON
【发布时间】:2011-10-25 13:08:58
【问题描述】:

我从服务提要中提取了以下 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":"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 之下。

标签: php json formatting


【解决方案1】:

在 JSON 中,大括号(“{”和“}”)定义对象,而不是数组。尖括号定义数组。

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

【讨论】:

    【解决方案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
                )
    
        )
    
    )
    

    请务必注意什么是对象和数组,以便使用适当的方法访问数据(因此,使用 -> 表示法的对象和带有 [] 表示法的数组)。

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 2018-01-27
      • 2018-05-14
      • 1970-01-01
      • 2014-07-11
      • 2011-10-21
      • 1970-01-01
      • 2017-06-17
      • 2013-11-15
      相关资源
      最近更新 更多