解析Twitter的Json对Trends的回应

时间:2013-08-16 10:52:52

标签: php javascript jquery json parsing

我正在尝试根据位置在Twitter上制作十大热门话题列表。这是我的JSON响应(当我使用echo json_encode($ data))

[{
    "trends": [{
        "name": "#For5Rs",
        "url": "http:\/\/twitter.com\/search?q=%23For5Rs",
        "promoted_content": null,
        "query": "%23For5Rs",
        "events": null
    }, {
        "name": "#RoadToSafety",
        "url": "http:\/\/twitter.com\/search?q=%23RoadToSafety",
        "promoted_content": null,
        "query": "%23RoadToSafety",
        "events": null
    }, {
        "name": "#CelebrateBlogging",
        "url": "http:\/\/twitter.com\/search?q=%23CelebrateBlogging",
        "promoted_content": null,
        "query": "%23CelebrateBlogging",
        "events": null
    }, {
        "name": "#WeWantMentalPoster",
        "url": "http:\/\/twitter.com\/search?q=%23WeWantMentalPoster",
        "promoted_content": null,
        "query": "%23WeWantMentalPoster",
        "events": null
    }, {
        "name": "#HouseFullDobaara",
        "url": "http:\/\/twitter.com\/search?q=%23HouseFullDobaara",
        "promoted_content": null,
        "query": "%23HouseFullDobaara",
        "events": null
    }, {
        "name": "Rajat Arora",
        "url": "http:\/\/twitter.com\/search?q=%22Rajat+Arora%22",
        "promoted_content": null,
        "query": "%22Rajat+Arora%22",
        "events": null
    }, {
        "name": "Sadhu Yadav",
        "url": "http:\/\/twitter.com\/search?q=%22Sadhu+Yadav%22",
        "promoted_content": null,
        "query": "%22Sadhu+Yadav%22",
        "events": null
    }, {
        "name": "Javed Bashir",
        "url": "http:\/\/twitter.com\/search?q=%22Javed+Bashir%22",
        "promoted_content": null,
        "query": "%22Javed+Bashir%22",
        "events": null
    }, {
        "name": "Milan Luthria",
        "url": "http:\/\/twitter.com\/search?q=%22Milan+Luthria%22",
        "promoted_content": null,
        "query": "%22Milan+Luthria%22",
        "events": null
    }, {
        "name": "Vidya Balan",
        "url": "http:\/\/twitter.com\/search?q=%22Vidya+Balan%22",
        "promoted_content": null,
        "query": "%22Vidya+Balan%22",
        "events": null
    }],
    "as_of": "2013-08-16T10:31:35Z",
    "created_at": "2013-08-16T10:20:41Z",
    "locations": [{
        "name": "India",
        "woeid": 23424848
    }]
}]

解析此问题的最佳方法是什么?我目前正在使用jQuery,并且我有一个脚本可以在搜索推文时解析JSON数据,但它是自定义的来处理推文。我只想要一份十大热门职位列表。

编辑:这是我到目前为止的代码。

<?php
require_once('config.php');
require_once('twitteroauth.php');
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/trends/place.json?id=23424848"); //This is hardcoded for now
echo json_encode($tweets);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Trend Grabber</title>
</head>

<body>


</body>
</html>

编辑3:var_dump($ tweets)的输出如下所示:

array (size=1)
  0 => 
    object(stdClass)[5]
      public 'trends' => 
        array (size=10)
          0 => 
            object(stdClass)[6]
              ...
          1 => 
            object(stdClass)[7]
              ...
          2 => 
            object(stdClass)[8]
              ...
          3 => 
            object(stdClass)[9]
              ...
          4 => 
            object(stdClass)[10]
              ...
          5 => 
            object(stdClass)[11]
              ...
          6 => 
            object(stdClass)[12]
              ...
          7 => 
            object(stdClass)[13]
              ...
          8 => 
            object(stdClass)[14]
              ...
          9 => 
            object(stdClass)[15]
              ...
      public 'as_of' => string '2013-08-19T13:36:34Z' (length=20)
      public 'created_at' => string '2013-08-19T13:26:17Z' (length=20)
      public 'locations' => 
        array (size=1)
          0 => 
            object(stdClass)[16]
              ...

这是在循环中使用print_r($ a)的输出(foreach($ tweets as $ a))

stdClass Object ( [trends] => Array ( [0] => stdClass Object ( [name] => #TimeFliesFast [url] => http://twitter.com/search?q=%23TimeFliesFast [promoted_content] => [query] => %23TimeFliesFast [events] => ) [1] => stdClass Object ( [name] => #AudiQ3s [url] => http://twitter.com/search?q=%23AudiQ3s [promoted_content] => [query] => %23AudiQ3s [events] => ) [2] => stdClass Object ( [name] => #DhamaalThingsILikeToClick [url] => http://twitter.com/search?q=%23DhamaalThingsILikeToClick [promoted_content] => [query] => %23DhamaalThingsILikeToClick [events] => ) [3] => stdClass Object ( [name] => #wineanddine [url] => http://twitter.com/search?q=%23wineanddine [promoted_content] => [query] => %23wineanddine [events] => ) [4] => stdClass Object ( [name] => #SiblingLuv [url] => http://twitter.com/search?q=%23SiblingLuv [promoted_content] => [query] => %23SiblingLuv [events] => ) [5] => stdClass Object ( [name] => Happy World Photography Day [url] => http://twitter.com/search?q=%22Happy+World+Photography+Day%22 [promoted_content] => [query] => %22Happy+World+Photography+Day%22 [events] => ) [6] => stdClass Object ( [name] => Audi [url] => http://twitter.com/search?q=Audi [promoted_content] => [query] => Audi [events] => ) [7] => stdClass Object ( [name] => Rakhi [url] => http://twitter.com/search?q=Rakhi [promoted_content] => [query] => Rakhi [events] => ) [8] => stdClass Object ( [name] => Manish Tewari [url] => http://twitter.com/search?q=%22Manish+Tewari%22 [promoted_content] => [query] => %22Manish+Tewari%22 [events] => ) [9] => stdClass Object ( [name] => Priyanka Chopra [url] => http://twitter.com/search?q=%22Priyanka+Chopra%22 [promoted_content] => [query] => %22Priyanka+Chopra%22 [events] => ) ) [as_of] => 2013-08-19T13:37:55Z [created_at] => 2013-08-19T13:26:17Z [locations] => Array ( [0] => stdClass Object ( [name] => India [woeid] => 23424848 ) ) ) 

1 个答案:

答案 0 :(得分:3)

以下内容将在表格中显示趋势。

<?php
$tweets = $connection->get("https://api.twitter.com/1.1/trends/place.json?id=23424848");
$trends = $tweets[0]->trends;

?>
<table>
<?php
foreach($trends as $trend){
?>
    <tr>
        <td><?php echo $trend->name; ?></td>
        <td><?php echo $trend->url; ?></td>
        <td><?php echo $trend->promoted_content; ?></td>
        <td><?php echo $trend->query; ?></td>
        <td><?php echo $trend->events; ?></td>
    </tr>
<?php    
}
?>
</table>

但请再次详细介绍在PHP中迭代数组,关联数组,多维数组和对象的基础知识。