作者:000猫000故事 | 来源:互联网 | 2023-05-18 00:20
ImusingjQuerys.getJSONfunctiontoparseasetofsearchresultsfromaGoogleSearchAppliance
I'm using jQuery's .getJSON function to parse a set of search results from a Google Search Appliance. The search appliance has an xslt stylesheet that returns the results as JSON data, which I validated with both JSONLint and Curious Concept's JSON Formatter.
我使用jQuery的. getjson函数来解析来自谷歌搜索设备的一组搜索结果。搜索设备有一个xslt样式表,它以JSON数据返回结果,我使用JSONLint和Curious Concept的JSON格式化程序对其进行了验证。
According to FireBug, the full result set is returned from the XMLHTTPRequest, but I tried dumping the data (with jquery.dump.js) and it only ever parses back the first result. It does successfully get all the Google Search Protocol stuff, but it only ever sees one "R" object (or individual result).
根据FireBug,从XMLHTTPRequest返回完整的结果集,但是我尝试转储数据(使用jquery.dump.js),它只解析第一个结果。它确实成功地获得了所有谷歌搜索协议,但是它只看到一个“R”对象(或单个结果)。
Has anybody had a similar problem with jQuery's .getJSON? I know it likes to fail silently if the JSON is not valid, but like I said, I validated the results with several validators and it should be good to go.
有人对jQuery的.getJSON有过类似的问题吗?我知道,如果JSON无效,它就会无声地失败,但就像我说的,我用几个验证器验证结果,应该很好。
Edit: Clicking this link will show you the JSON results returned for a search for the word "google": http://bigbird.uww.edu/search?client=json_frontend&proxystylesheet=json_frontend&proxyrefresh=1&output=xml_no_dtd&q=google
编辑:点击这个链接将显示为搜索“谷歌”返回的JSON结果:http://bigbird.uww.edu/search?
jQuery only retrieves the first "R" object, even though all "R" objects are siblings.
jQuery只检索第一个“R”对象,即使所有的“R”对象都是兄弟对象。
2 个解决方案
2
You might try doing "getJSON" yourself with your own "jsonpCallback" function. If the response from the API you're calling looks like a comma-separated list of JSON expressions, then the jQuery automatically-constructed callback function will only see the first one.
您可以尝试使用自己的“jsonpCallback”函数来实现“getJSON”。如果您正在调用的API的响应看起来像一个逗号分隔的JSON表达式列表,那么jQuery自动构造的回调函数将只看到第一个。
In other words, if the API returns
换句话说,如果API返回
{something: "foo", whatever:23}, {something: "bar", whatever, 32}
then what'll end up in the response script block is:
然后在响应脚本块中结束的是:
magicJqueryCallback({something: "foo", whatever:23}, {something: "bar", whatever, 32})
The jQuery callback is declared as having just one argument, which it assigns to the "data" element of the fake XHR object.
jQuery回调声明为只有一个参数,它将该参数赋给伪XHR对象的“data”元素。
Alternatively, if you have control over what the XSLT code does, you could have it wrap the list of responses in a set of square brackets, before it even gets to jQuery:
或者,如果您能够控制XSLT代码的功能,您可以让它将响应列表封装在一组方括号中,甚至在它到达jQuery之前:
[{something: "foo", whatever:23}, {something: "bar", whatever, 32}]
If your XSLT produced that, it would (I hope) work just fine with getJSON.
如果您的XSLT产生了这个特性,那么(我希望)它对getJSON来说就可以工作得很好。
edit OK, I see your problem now.
好的,我看到你的问题了。
Your JSON response contains multiple values for "R" inside the outer object. That's not going to work: if "R" is a list, it needs to have a single value, with that value being an array.
您的JSON响应包含外部对象中“R”的多个值。这是行不通的:如果“R”是一个列表,它需要有一个值,这个值就是一个数组。
{"GSP": ..., "R":[{"U": ... }, {"U": ... }, {"U": ...}], ...}