jQuery .getJSON() 未解析所有对象
我正在使用 jQuery 的 .getJSON 函数来解析来自 Google Search Appliance 的一组搜索结果。 Search Appliance 有一个 xslt 样式表,它以 JSON 数据的形式返回结果,我使用 JSONLint 和 Curious Concept 的 JSON Formatter 对其进行了验证。
根据 FireBug 的说法,完整的结果集是从 XMLHTTPRequest 返回的,但我尝试转储数据(使用 jquery.dump.js),它只解析回第一个结果。它确实成功获取了所有 Google 搜索协议内容,但它只看到一个“R”对象(或单个结果)。
有人对 jQuery 的 .getJSON 有类似的问题吗?我知道如果 JSON 无效,它会默默地失败,但正如我所说,我使用多个验证器验证了结果,应该可以正常进行。
编辑: 单击此链接将显示搜索单词“google”返回的 JSON 结果:http://bigbird.uww.edu/search?client=json_frontend&proxystylesheet=json_frontend&proxyrefresh=1&output=xml_no_dtd&q=google
jQuery仅检索第一个“R”对象,即使所有“R”对象都是同级对象。
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.
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).
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.
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
jQuery only retrieves the first "R" object, even though all "R" objects are siblings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用自己的“jsonpCallback”函数自己执行“getJSON”。如果您调用的 API 的响应看起来像一个以逗号分隔的 JSON 表达式列表,那么 jQuery 自动构造的回调函数将只能看到第一个。
换句话说,如果 API 返回,
那么响应脚本块中最终的结果是:
jQuery 回调被声明为只有一个参数,并将该参数分配给伪造的 XHR 对象的“data”元素。
或者,如果您可以控制 XSLT 代码的功能,则可以让它在到达 jQuery 之前将响应列表包装在一组方括号中:
如果您的 XSLT 生成了该代码,那么它(我希望)可以正常工作使用 getJSON 没问题。
编辑 好的,我现在看到你的问题了。
您的 JSON 响应在外部对象内包含多个“R”值。这是行不通的:如果“R”是一个列表,它需要有一个单个值,该值是一个数组。
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.
In other words, if the API returns
then what'll end up in the response script block is:
The jQuery callback is declared as having just one argument, which it assigns to the "data" element of the fake XHR object.
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:
If your XSLT produced that, it would (I hope) work just fine with 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.
或者,您始终可以只使用 $.ajax 函数,然后简单地评估生成的 JSON。我意识到这通常是不明智的,但由于您可以确定 Google Search Appliance 不会注入任何类型的攻击,因此在这种情况下可以使用它。
Alternatively you could always just use the $.ajax function and then simply eval the resulting JSON. I realize this is normally ill-advised but since you can be certain the Google Search Appliance won't inject an attack of any kind it could be used in this case.