我如何解析谷歌的建议查询JSON响应?

时间:2021-07-28 14:52:45

Before the following URL gave a JSON output: http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&q=Query

在以下网址提供JSON输出之前:http://suggestqueries.google.com/complete/search?client = youroutube&ds = yt&q = Query

now it outputs the following:

现在它输出以下内容:

window.google.ac.h(["Query",[["query",0],["query optimization",0],["query in access 2013",0],["query processing and optimization",0],["query optimization in dbms",0],["querying microsoft sql server 2012",0],["query in access 2010",0],["query letter",0],["querying microsoft sql server 2012 tutorial",0],["query access",0]],{"k":1,"q":"4-l7QUSZEiiQKaSq-yXfrtfHpd0"}])

Curious as to how i can parse this into JSON

好奇我如何将其解析为JSON

1 个解决方案

#1


That's JSONP.

If you were to call this using JSONP technique (adding <script> tag to your page with the src= attribute set to the URL) then you would need to declare the function window.google.ac.h before making the JSONP call to process the result.

如果您使用JSONP技术调用此方法(将

If you were to call this using ajax or on the server then you have two options:

如果你使用ajax或服务器调用它,那么你有两个选择:

  1. declare the funciton window.google.ac.h to process the result then eval the response. (Because, that's what adding a <script> tag does, it evals the javascript file in your page. So you're basically just emulating JSONP)

    声明函数window.google.ac.h来处理结果然后评估响应。 (因为,这就是添加

    function window.google.ac.h (json) {
        // process your response here
    }
    var s = document.createElement('script');
    s.src = 'http://suggestqueries.google.com/complete/search?' +
            'client=youtube&ds=yt&q=Query';
    document.body.appendChild(s);
    

    alternatively, if you receive the response via other means:

    或者,如果您通过其他方式收到回复:

    function window.google.ac.h (json) {
        // process your response here
    }
    eval(response);
    
  2. Remove the outer window.google.ac.h( .. and .. ) from the response then parse it as JSON.

    从响应中删除外部window.google.ac.h(..和..),然后将其解析为JSON。

    var json = response.replace(/^.*?\(/,'').replace(/\)$/,'');
    

#1


That's JSONP.

If you were to call this using JSONP technique (adding <script> tag to your page with the src= attribute set to the URL) then you would need to declare the function window.google.ac.h before making the JSONP call to process the result.

如果您使用JSONP技术调用此方法(将

If you were to call this using ajax or on the server then you have two options:

如果你使用ajax或服务器调用它,那么你有两个选择:

  1. declare the funciton window.google.ac.h to process the result then eval the response. (Because, that's what adding a <script> tag does, it evals the javascript file in your page. So you're basically just emulating JSONP)

    声明函数window.google.ac.h来处理结果然后评估响应。 (因为,这就是添加

    function window.google.ac.h (json) {
        // process your response here
    }
    var s = document.createElement('script');
    s.src = 'http://suggestqueries.google.com/complete/search?' +
            'client=youtube&ds=yt&q=Query';
    document.body.appendChild(s);
    

    alternatively, if you receive the response via other means:

    或者,如果您通过其他方式收到回复:

    function window.google.ac.h (json) {
        // process your response here
    }
    eval(response);
    
  2. Remove the outer window.google.ac.h( .. and .. ) from the response then parse it as JSON.

    从响应中删除外部window.google.ac.h(..和..),然后将其解析为JSON。

    var json = response.replace(/^.*?\(/,'').replace(/\)$/,'');