【问题标题】:Parsing twitter's Json response for Trends解析 Twitter 对 Trends 的 Json 响应
【发布时间】:2013-08-16 10:52:52
【问题描述】:

我正在尝试根据位置在 Twitter 上列出前 10 个热门话题。这是我的 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 数据,但它是为处理推文而定制的。我只想要一个前 10 名热门标题的列表。

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

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

【问题讨论】:

  • 您在 PHP 中对其进行编码以及在 jQuery 中如何分配它?如果是 jQuery 的情况,您可以使用 $.parseJSON 并遍历返回的对象。
  • 对不起,我的意思是我使用 jQuery 来解析我在按标签搜索推文时得到的 JSON。我的整个页面都是用 PHP 编写的,我正在代码中进行编辑。
  • 您将获得 $tweets 作为一个数组。您只需要遍历它即可列出推文。
  • 我尝试这样做,但在解析时出现错误,我已通过编辑更新了信息。 @Jithin
  • $a 在您的迭代中不是字符串。因此,不要尝试echo,而是尝试print_r 或只是var_dump

标签: php javascript jquery json parsing


【解决方案1】:

以下将以表格的形式显示趋势。

<?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 中迭代数组、关联数组、多维数组和对象的基础知识。

【讨论】:

    猜你喜欢
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多