2012-10-01 94 views
2

首先,我對PHP非常陌生,所以我仍在嘗試。我的問題是:我試圖在PHP中迴應Bing API結果。顯示json結果的PHP問題

這裏是一個JSON結果例子,這是我從

$jsonobj = json_decode($response); 

得到(的反應是我從兵得到的,所以我只粘貼了以下回應 - 萬一添加此信息你不知道在哪裏得到$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; 
} 

但是,我迷失在如何迴應實際結果,如Title,DescriptionUrl

我想:

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

,因爲我認爲添加Web到foreach將可能呼應正確的價值觀,但沒有成功。

也許有人可以幫助我,告訴我我做錯了什麼?

我的任務將有如下結果:

標題:PHP:Hypertext Preprocessor的

描述:PHP是什麼? PHP是一種廣泛使用的通用 腳本語言,特別適合Web開發,並且 可以嵌入到HTML中。

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

然後,其他兩個結果。

太謝謝你了:)

+0

有時,它幫助打印出對象的格式化版本,以更好地瞭解這些元素。 :)'printf('

%s
',print_r($ value,1));' – Sean3z

回答

5

你可以做到這一點,如下所示:

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

通過方式:你應該使用一個工具來格式化你的json字符串。通過這種方式,json數據將如何變得更加清晰。

+0

演示:http://codepad.org/av383ySl :) – Sean3z

+0

謝謝你幫助的鍵盤演示:) –

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 />'; 
} 
0

首先使用print_r方法,這樣你就可以看到對象是怎麼樣的,所以你可以確定從哪裏開始循環。

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

然後將類型更改爲像數組$ array =(array)$ jsonobj;所以你可以用它來array_shift功能(你可以使用此功能調整陣列)

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

+0

這有什麼用? – Sean3z

+0

它將幫助您首先調試數組,以便您輕鬆知道從何處開始循環,而不是使用長方法鏈接 –

+0

請參閱我的第一條評論。只需打印出對象即可完成此操作。不需要recast和array_shift();) – Sean3z