【问题标题】:How to parse google custom search result get throug Json by using restfull api to Google results as google shows?如何通过使用restful api来解析通过Json获得的谷歌自定义搜索结果到谷歌显示的谷歌结果?
【发布时间】:2013-06-17 04:22:31
【问题描述】:

我想将通过 google 自定义搜索 restful api 获取的 json 解析为 google 在其搜索结果中显示的结果。

我正在使用 ajax 进行服务调用,但不知道如何在 Google 搜索结果格式中解析结果... 这是我的代码

    <script>
$(document).ready(function (){
      $.ajax({
        url: https:www.googleapis.com/customsearch/v1?key=AIzaSyDETpXCuhV0RsUtqiM5i8GB0EZ_v_bLNLs&cx=013036536707430787589:_pqjad5hr1a&q=<?php echo $val; ?>&alt=json,
        dataType: 'json',
        data: data,
        success: function(json_data){
          //Whats Efficient Way to Parse the Code In HTML as
          //Google SHows In search Results
          alert(data);                  
        }); 
      });
    </script>

【问题讨论】:

    标签: jquery json parsing google-custom-search


    【解决方案1】:

    您可以按如下方式解析您的 json_data:

    alert(jQuery.parseJSON(json_data));
    

    【讨论】:

      【解决方案2】:

      我们也可以使用 jquery 来实现这个...

          <script type="text/javascript" src="jquery-1.7.1.js"></script>
      <script type="text/javascript" >
      $(document).ready(function(){
      
                  var data = GetURLParameter('search_data'); //that data we passed for google search
      
                  $.getJSON('https://www.googleapis.com/customsearch/v1?key=your key&cx=YOUR CX&q='+data, function(resp)
                  {
      
                       var i=0;
      
                       while(i<=10)
                      {
                          var title=resp.items[i].title;
                          var link=resp.items[i].link;
      
      
                          alert(title);
      
                          i++;
                      }
      
                  });
      });
      
      function GetURLParameter(sParam)
      {
          var sPageURL = window.location.search.substring(1);
          var sURLVariables = sPageURL.split('&');
          for (var i = 0; i < sURLVariables.length; i++)
          {
              var sParameterName = sURLVariables[i].split('=');
              if (sParameterName[0] == sParam)
              {
                  return decodeURIComponent(sParameterName[1]);
              }
          }
      }
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        • 2014-08-25
        • 2013-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多