Php 如何处理JSON返回数组?

Php 如何处理JSON返回数组?,php,jquery,json,Php,Jquery,Json,我已经学会了很多XML处理,但我对JSON一窍不通,不知道如何用PHP(或jquery)解析以下数据。 如果我是 $var = file_get_contents("http://wthashtag.com/api/v2/trends/active.json"); (这是趋势数据的Twitter JSON返回) 那么 URL是json中的URL变量,Blah是name变量,这里的text是指json中的text:variable 如何获取json的输入,并将其输出为: (此处文本) 这让我很困

我已经学会了很多XML处理,但我对JSON一窍不通,不知道如何用PHP(或jquery)解析以下数据。 如果我是

$var = file_get_contents("http://wthashtag.com/api/v2/trends/active.json");
(这是趋势数据的Twitter JSON返回) 那么

URL是json中的URL变量,Blah是name变量,这里的text是指json中的text:variable 如何获取json的输入,并将其输出为:


(此处文本)


这让我很困惑:我在SO和Google以及PHP手册上尝试了其他一些响应,但都没有成功的结果,我能得到的最好结果是将$obj作为json解码字符串,并在每个地方使用对象(stdclass)数组。

当使用
json\u decode
时,您正在创建一个数组。在
json\u decode($var)
上执行
print\u r
,您将获得多维数组结构,然后可以执行以下操作:

echo $jsonArray['something']['text'];

您可以使
json\u decode()
返回对象或数组(通过将第二个参数设置为
true
)。之后,您可以使用该对象/数组中的值(在您的案例中为
$obj
)进行进一步处理。例如:

foreach ( $obj->trends as $trend ) {
    echo '<a href="' . $trend['url'] . '">' . $trend['name'] . '</a>';
}
foreach($obj->趋势为$trend){
回声';
}

这是一个在json\u解码后接收到的对象

<?php
$object = json_decode(file_get_contents("http://wthashtag.com/api/v2/trends/active.json"));

foreach($object->trends as $trend){
    echo '<a href="'.$trend->url.'">'.$trend->name.'</a><br />'.$trend->description->text.'<br />';
}
?>

给你

<?php

$var = file_get_contents("http://wthashtag.com/api/v2/trends/active.json");
$json = json_decode($var);
foreach($json->trends as $lol){
      echo "<a href=".$lol->url.">".$lol->name."</a><br>";
}

?>

如果请求失败、解码失败、数组成员为空,仍然需要添加错误处理
<?php

$var = file_get_contents("http://wthashtag.com/api/v2/trends/active.json");
$json = json_decode($var);
foreach($json->trends as $lol){
      echo "<a href=".$lol->url.">".$lol->name."</a><br>";
}

?>