如何反序列化json结果以HTML / CSS样式格式显示它们?

时间:2012-08-13 01:12:35

标签: php api json

我刚开始使用Google API for Custom Search Engine,我已经成功完成了第一个请求:

My_first_Search.php 的      

 require_once 'apis/apiClient.php';
 require_once 'apis/contrib/apiCustomsearchService.php';
 session_start();

 $client = new apiClient();
 $client->setApplicationName('Google CustomSearch PHP Starter Application');
 // Docs: http://code.google.com/apis/customsearch/v1/using_rest.html
 // My developer key (simple api key).
 $client->setDeveloperKey('***********************************');
 $search = new apiCustomsearchService($client);


  // executing a search with your custom search id.
  $result = $search->cse->listCse('burrito', array(
  'cx' => '123456789123546789:*******', // The custom search engine ID to scope this search query.
  ));
 print "<pre>" . print_r($result, true) . "</pre>";

 // executing a search with the URL of a linked custom search engine.
 $result = $search->cse->listCse('burrito', array(
 'cref' => 'http://www.google.com/cse/samples/vegetarian.xml',
  ));
 print "<pre>" . print_r($result, true) . "</pre>";

输出 This file以JSON格式正确输出关键字burrito的结果

我如何脱盐然后使用for-each循环来操作结果:

  - Result title
  - Result description
  - URL

Click here to see the JSON output to be manipulated

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

你的“这个文件”没有输出json。 JSON是JS中变量赋值的文本表示。例如var x = ...json here...的右侧。该转储是一个PHP数据结构,它是通过解码json字符串生成的。

换句话说,不要挂断你正在获得JSON的事实 - 一旦它被解码,它只是另一个PHP数组,你使用普通的PHP数组操作和索引来获取你的数据。

e.g。

$arr['queries']['nextPage'][0]['title'] // Google Custom Search - burrito