我正在对URL执行GET php函数,然后在JSON中返回结果响应:
{
"results": [{
"text": "The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 - 30.",
"createdAt": "2014-02- 24T19:54:08.707Z",
"updatedAt": "2014-02-24T19:54:08.707Z",
"objectId": "ZZrZ9OgyRG"
}, {
"text": "Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!",
"createdAt": "2014-03-24T13:23:56.240Z",
"updatedAt": "2014-03-24T13:23:56.240Z",
"objectId": "ZqxJRiHoJo"
}]
}
我可以执行php json_decode
并在下面的网页上显示以下结果
text | The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 - 30.
createdAt | 2014-02-24T19:54:08.707Z
updatedAt | 2014-02-24T19:54:08.707Z
objectId | ZZrZ9OgyRG
text | Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!
createdAt | 2014-03-24T13:23:56.240Z
updatedAt | 2014-03-24T13:23:56.240Z
objectId | ZqxJRiHoJo
我用来在网页上显示上述结果的php代码是:
$returned_content = get_data('https://api.parse.com/1/classes/Alerts');
$data = json_decode($returned_content, true);
foreach ($data as $array1 => $arrayn) {
foreach ($arrayn as $k => $v) {
foreach ($v as $t => $s) {
echo"<p> $t | $s ";
}
}
}
如果我只是想显示&#39;文字&#39;仅在网页上的键/值信息,我应该如何修改php。我希望在网页上看到的所有内容都是:
text | The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 – 30.
text | Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!
答案 0 :(得分:1)
$data = json_decode($json);
foreach ($data->results as $item) {
echo '<br>text | '.$item->text;
}
如果你没有#39;添加第二个json_decode()
参数,你可以将你的JSON与StdClass一起使用。
答案 1 :(得分:0)
将其包裹在简单的if条件中。
foreach ($data as $array1 => $arrayn) {
foreach($arrayn as $k => $v) {
foreach ($v as $t => $s) {
if($t == 'text') {
echo"<p> $t | $s ";
}
}
}
}
答案 2 :(得分:0)
喜欢这个
$returned_content = get_data('https://api.parse.com/1/classes/Alerts');
$data = json_decode($returned_content, true);
foreach ($data as $array1 => $arrayn) {
foreach($arrayn as $k => $v){
foreach ($v as $t => $s)
{
if($t == "text")
echo"<p> $t | $s ";
}
}
}
答案 3 :(得分:0)
这一行之后:
$data = json_decode($returned_content, true);
您有一个关联数组,因此您可以将循环更改为:
foreach ($data as $array1 => $arrayn) {
foreach($arrayn as $k => $v) {
foreach ($v as $t => $s) {
if('text' === $t) {
echo"<p> $t | $s ";
}
}
}
}
答案 4 :(得分:0)
简化并检查密钥是否存在,以防万一JSON不包含密钥&#34; text&#34;它将返回通知。
$data = json_decode($str,true);
foreach($data["results"] as $key=>$val){
if(array_key_exists("text",$val)){
echo 'text | '.$val["text"];
echo '<br />';
}
}
这里$str
是你的json字符串。