在Javascript中使用JSON从Google Search Ajax API获取结果

时间:2022-08-22 10:19:26

I am trying to obtain the url from the innerHTML of a web page using javascript.

我试图使用javascript从网页的innerHTML获取网址。

This is what I'm doing:

这就是我正在做的事情:

var goog = newTabBrowser.contentDocument.getElementsByTagName("pre")[0].innerHTML;
   alert(goog.responseData.results[0].url);

BUT it wont work :S

但它不会工作:S

I outputted goog (using alert) and copied that into my program as follows:

我输出了goog(使用alert)并将其复制到我的程序中,如下所示:

var goog =  {"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://*.com/","url":"http://*.com/","visibleUrl":"*.com","cacheUrl":"http://www.google.com/search?q\u003dcache:U1GC2GYOToIJ:*.com","title":"\u003cb\u003eStack Overflow\u003c/b\u003e","titleNoFormatting":"Stack Overflow","content":"A language-independent collaboratively edited question and answer site for  programmers."},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://en.wikipedia.org/wiki/Stack_overflow","url":"http://en.wikipedia.org/wiki/Stack_overflow","visibleUrl":"en.wikipedia.org","cacheUrl":"http://www.google.com/search?q\u003dcache:mWu8b0BQAmwJ:en.wikipedia.org","title":"\u003cb\u003eStack overflow\u003c/b\u003e - Wikipedia, the free encyclopedia","titleNoFormatting":"Stack overflow - Wikipedia, the free encyclopedia","content":"In software, a \u003cb\u003estack overflow\u003c/b\u003e occurs when too much memory is used on the call  \u003cb\u003estack\u003c/b\u003e. In many programming languages the call \u003cb\u003estack\u003c/b\u003e contains a limited amount  \u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://blog.*.com/","url":"http://blog.*.com/","visibleUrl":"blog.*.com","cacheUrl":"http://www.google.com/search?q\u003dcache:iqtvg9Ge1c0J:blog.*.com","title":"Blog - \u003cb\u003eStack Overflow\u003c/b\u003e","titleNoFormatting":"Blog - Stack Overflow","content":"Apr 12, 2009 \u003cb\u003e...\u003c/b\u003e Apparently some users who really should know better are confused about the way  \u003cb\u003eStack Overflow\u003c/b\u003e works. I take this as a sweeping indictment of \u003cb\u003e...\u003c/b\u003e"},{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://support.microsoft.com/kb/145799","url":"http://support.microsoft.com/kb/145799","visibleUrl":"support.microsoft.com","cacheUrl":"","title":"How to Troubleshoot Windows Internal \u003cb\u003eStack Overflow\u003c/b\u003e Error Messages","titleNoFormatting":"How to Troubleshoot Windows Internal Stack Overflow Error Messages","content":"This article lists steps to help you troubleshoot problems with \u003cb\u003estack overflow\u003c/b\u003e  errors in Windows. Stacks are reserved memory that programs use to process \u003cb\u003e...\u003c/b\u003e"}],"cursor":{"pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"273000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dStack+overflow"}}, "responseDetails": null, "responseStatus": 200};`

and tried

alert(goog.responseData.results[0].url);

and this worked

这很有效

2 个解决方案

#1


innerHTML returns a string—not an object structure. You will probably have to use eval or a JSON parsing library (depending on how secure the JSON data is) to convert it from a string into a JavaScript object.

innerHTML返回一个字符串 - 而不是一个对象结构。您可能必须使用eval或JSON解析库(取决于JSON数据的安全性)将其从字符串转换为JavaScript对象。

For example, (using the json2.js JSON library):

例如,(使用json2.js JSON库):

var googString = newTabBrowser.contentDocument.getElementsByTagName("pre")[0].innerHTML;
var goog = JSON.parse(googString);

// This should give the correct result
alert(goog.responseData.results[0].url);

Steve

#2


Check this out someone has wrote jquery.google plugin I know this plugin to small and it doesn't support paging or like that. I think it will be help you ? Just check it's source code.

检查一下有人写了jquery.google插件我知道这个插件很小,它不支持分页或类似。我想这对你有帮助吗?只需检查它的源代码。

Regards

#1


innerHTML returns a string—not an object structure. You will probably have to use eval or a JSON parsing library (depending on how secure the JSON data is) to convert it from a string into a JavaScript object.

innerHTML返回一个字符串 - 而不是一个对象结构。您可能必须使用eval或JSON解析库(取决于JSON数据的安全性)将其从字符串转换为JavaScript对象。

For example, (using the json2.js JSON library):

例如,(使用json2.js JSON库):

var googString = newTabBrowser.contentDocument.getElementsByTagName("pre")[0].innerHTML;
var goog = JSON.parse(googString);

// This should give the correct result
alert(goog.responseData.results[0].url);

Steve

#2


Check this out someone has wrote jquery.google plugin I know this plugin to small and it doesn't support paging or like that. I think it will be help you ? Just check it's source code.

检查一下有人写了jquery.google插件我知道这个插件很小,它不支持分页或类似。我想这对你有帮助吗?只需检查它的源代码。

Regards