gpt4 book ai didi

php - 处理 PHP JSON 对象中的数据

转载 作者:IT老高 更新时间:2023-10-28 11:54:54 25 4
gpt4 key购买 nike

来自 Twitter 搜索 API 的 JSON 格式的趋势数据。

使用以下方法获取文件:

$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

如何处理来自该对象的数据。作为一个数组?只需要从 [name] 值中提取数据。

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
)

最佳答案

你的意思是这样的?

<?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";
}

关于php - 处理 PHP JSON 对象中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/263392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com