显示json结果的PHP问题

时间:2012-10-01 20:48:45

标签: php json

首先,我是PHP的新手,所以我还在努力。我的问题是:我试图在PHP中回应Bing API结果。

这是一个JSON结果示例,我从

获取
$jsonobj = json_decode($response);

(响应是我从Bing得到的,所以我只是粘贴了下面的回复 - 只是添加这些信息,以防你想知道我从哪里获得$jsonobj = json_decode($response);

{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}

现在,据我所知,我可以通过使用:

来回应WebTotal
foreach($jsonobj->d->results as $value) {
    echo $value->WebTotal;
}

但是,我对如何回应TitleDescriptionUrl等实际结果感到迷茫。

我试过了:

foreach($jsonobj->d->results as $value) {
    echo $value->Title."<br>";
    echo $value->Description."<br>";
    echo $value->Url."<br>";
}

还有类似的东西:

foreach($jsonobj->d->results->Web as $value) {
    echo $value->Title."<br>";
    echo $value->Description."<br>";
    echo $value->Url."<br>";
}

因为我认为将网络添加到foreach可能会回应正确的值,但没有成功。

也许有人可以帮助我并告诉我我做错了什么?

我的任务是获得以下结果:

  

标题:PHP:超文本预处理器

     

描述:什么是PHP? PHP是一种广泛使用的通用目的   脚本语言,特别适合Web开发和   可以嵌入到HTML中。

     

的URL:http://www.php.net/

然后,另外两个结果。

非常感谢你:)

3 个答案:

答案 0 :(得分:5)

您可以按照以下方式执行此操作:

foreach($jsonobj->d->results as $result) {
    foreach($result->Web as $value) {
        echo $value->Title;
        // and the same for the other properties
    }
}

顺便说一句:您应该使用工具来格式化您的json字符串。通过这种方式,可以更清楚地了解json数据的样子。

答案 1 :(得分:0)

results包含一个数组,所以你需要这样的东西:

foreach($jsonobj->d->results[0]->Web as $value)
{
    echo 'Title: ' . $value->Title . '<br /><br />';
    echo 'Description: ' . $value->Description . '<br /><br />';
    echo 'Url: ' . $value->Url . '<br /><br /><br />';
}

答案 2 :(得分:0)

首先使用print_r方法,这样你就可以看到对象是怎样的,因此你可以确定从哪里开始循环。

echo '<pre>';
print_r($jsonobj);

然后将类型更改为数组,如$ array =(array)$ jsonobj;所以你可以将它用于array_shift函数(你可以使用这个函数调整你的数组)

http://php.net/manual/en/function.array-shift.php

相关问题