Trends data from Twitter Search API in JSON.
来自JSON的Twitter搜索API的趋势数据。
Grabbing the file using:
把文件使用:
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
How do I work with data from this object. As an array? Only really need to extract data from the [name] values.
如何处理来自这个对象的数据。作为一个数组?只需要从[name]值中提取数据。
JSON object contains:
JSON对象包含:
stdClass Object
(
[trends] => Array
(
[0] => stdClass Object
(
[name] => Vote
[url] => http://search.twitter.com/search?q=Vote
)
[1] => stdClass Object
(
[name] => Halloween
[url] => http://search.twitter.com/search?q=Halloween
)
[2] => stdClass Object
(
[name] => Starbucks
[url] => http://search.twitter.com/search?q=Starbucks
)
[3] => stdClass Object
(
[name] => #flylady
[url] => http://search.twitter.com/search?q=%23flylady
)
[4] => stdClass Object
(
[name] => #votereport
[url] => http://search.twitter.com/search?q=%23votereport
)
[5] => stdClass Object
(
[name] => Election Day
[url] => http://search.twitter.com/search?q=%22Election+Day%22
)
[6] => stdClass Object
(
[name] => #PubCon
[url] => http://search.twitter.com/search?q=%23PubCon
)
[7] => stdClass Object
(
[name] => #defrag08
[url] => http://search.twitter.com/search?q=%23defrag08
)
[8] => stdClass Object
(
[name] => Melbourne Cup
[url] => http://search.twitter.com/search?q=%22Melbourne+Cup%22
)
[9] => stdClass Object
(
[name] => Cheney
[url] => http://search.twitter.com/search?q=Cheney
)
)
[as_of] => Mon, 03 Nov 2008 21:49:36 +0000
)
4 个解决方案
#1
147
You mean something like this?
你是说像这样的东西?
<?php
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->trends as $trend )
{
echo "{$trend->name}\n";
}
#2
35
If you use json_decode($string, true)
, you will get no objects, but everything as an associative or number indexed array. Way easier to handle, as the stdObject provided by PHP is nothing but a dumb container with public properties, which cannot be extended with your own functionality.
如果您使用json_decode($string, true),您将不会得到任何对象,而是所有对象都作为一个关联数组或数字索引数组。很容易处理,因为PHP提供的stdObject只是一个具有公共属性的哑容器,不能使用自己的功能对其进行扩展。
$array = json_decode($string, true);
echo $array['trends'][0]['name'];
#3
8
Just use it like it was an object you defined. i.e.
就像你定义的对象一样使用它。即。
$trends = $json_output->trends;
#4
-2
The clean way would be:
清洁的方法是:
$jsonurl = 'http://search.twitter.com/trends.json';
$json = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json, true);
$trends = $json_output['trends'];
foreach ($trends as $trend) {
your_func($trend['name']);
}
#1
147
You mean something like this?
你是说像这样的东西?
<?php
$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->trends as $trend )
{
echo "{$trend->name}\n";
}
#2
35
If you use json_decode($string, true)
, you will get no objects, but everything as an associative or number indexed array. Way easier to handle, as the stdObject provided by PHP is nothing but a dumb container with public properties, which cannot be extended with your own functionality.
如果您使用json_decode($string, true),您将不会得到任何对象,而是所有对象都作为一个关联数组或数字索引数组。很容易处理,因为PHP提供的stdObject只是一个具有公共属性的哑容器,不能使用自己的功能对其进行扩展。
$array = json_decode($string, true);
echo $array['trends'][0]['name'];
#3
8
Just use it like it was an object you defined. i.e.
就像你定义的对象一样使用它。即。
$trends = $json_output->trends;
#4
-2
The clean way would be:
清洁的方法是:
$jsonurl = 'http://search.twitter.com/trends.json';
$json = file_get_contents($jsonurl, 0, null, null);
$json_output = json_decode($json, true);
$trends = $json_output['trends'];
foreach ($trends as $trend) {
your_func($trend['name']);
}