dongliqian6245 2012-08-13 01:12
浏览 126
已采纳

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

I just got started with Google API for Custom Search Engine, and I have succeded on doing my first request like this:

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

The output: This file output the results correctly for the keyword burrito in a JSON format

How can I desalinize then manipulate the result using a for-each loop to get:

  - Result title
  - Result description
  - URL

Click here to see the JSON output to be manipulated

Any help would be highly appreciated.

  • 写回答

1条回答 默认 最新

  • dphfwzn8269 2012-08-13 01:16
    关注

    your "this file" is not outputting json. JSON is the textual representation of a variable assignment in JS,. e.g. the right-hand side of var x = ...json here.... That dump is a PHP data structure, which has been produced by decoding a json string.

    in other words, don't get hung up on the fact that you're getting JSON - once it's decoded, it's just another PHP array, and you use normal PHP array operations and indexes to get at your data.

    e.g.

    $arr['queries']['nextPage'][0]['title'] // Google Custom Search - burrito
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题