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


How Deserialize json result to display them on HTML / CSS Styled format?

我刚刚开始使用用于自定义搜索引擎的Google API,并且我已经成功地完成了我的第一个请求,如下所示:

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

输出:此文件以 JSON 格式正确输出关键字burrito的结果

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

  - Result title
  - Result description
  - URL

单击此处查看要操作的 JSON 输出

任何帮助将不胜感激。

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

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

例如

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