PHP高级JSON代码

时间:2014-07-04 20:56:04

标签: php json

如何从这个JSON代码获取PHP“1837”?有人可以帮助我,我不明白。 :(因为有超过1“countHint”,我无法获得“1837”值。

{
   "gd$feedLink":[
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.subscriptions",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/subscriptions?v=2",
         "countHint":68
      },
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.liveevent",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/live/events?v=2",
         "countHint":0
      },
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.favorites",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/favorites?v=2",
         "countHint":795
      },
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.contacts",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/contacts?v=2",
         "countHint":9915
      },
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.inbox",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/inbox?v=2"
      },
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.playlists",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/playlists?v=2"
      },
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.uploads",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/uploads?v=2",
         "countHint":1837
      },
      {
         "rel":"http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos",
         "href":"http://gdata.youtube.com/feeds/api/users/pewdiepie/newsubscriptionvideos?v=2"
      }
   ]
}

2 个答案:

答案 0 :(得分:1)

您可以将JSON数据解码为PHP数组或对象,然后使用普通的PHP数组函数按特定值进行过滤。 (经常在SO上解释后者。)

$json = '{ "gd$feedLink":[ … ] }';
$phpArray = json_decode( $json, true );

答案 1 :(得分:0)

就像费拉描述的那样使用json_decode这里是完整的代码:D enjoy

$json = '{
    "gd$feedLink": [
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.subscriptions",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/subscriptions?v=2",
            "countHint": 68
        },
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.liveevent",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/live/events?v=2",
            "countHint": 0
        },
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.favorites",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/favorites?v=2",
            "countHint": 795
        },
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.contacts",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/contacts?v=2",
            "countHint": 9915
        },
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.inbox",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/inbox?v=2"
        },
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.playlists",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/playlists?v=2"
        },
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.uploads",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/uploads?v=2",
            "countHint": 1837
        },
        {
            "rel": "http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos",
            "href": "http://gdata.youtube.com/feeds/api/users/pewdiepie/newsubscriptionvideos?v=2"
        }
    ]
}';

$decoded = json_decode($json,true);

foreach ($decoded['gd$feedLink'] as $item  ){
    if( isset($item['countHint']) && $item['countHint']=='1837'){
        echo $item['rel']."<br/>";
        echo $item['href']."<br/>"; 
        echo $item['countHint']."<br/>";

    }

}