jQuery .getJSON() 未解析所有对象

发布于 2024-09-02 03:45:12 字数 811 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所谓喜欢 2024-09-09 03:45:12

您可以尝试使用自己的“jsonpCallback”函数自己执行“getJSON”。如果您调用的 API 的响应看起来像一个以逗号分隔的 JSON 表达式列表,那么 jQuery 自动构造的回调函数将只能看到第一个。

换句话说,如果 API 返回,

{something: "foo", whatever:23}, {something: "bar", whatever, 32}

那么响应脚本块中最终的结果是:

magicJqueryCallback({something: "foo", whatever:23}, {something: "bar", whatever, 32})

jQuery 回调被声明为只有一个参数,并将该参数分配给伪造的 XHR 对象的“data”元素。

或者,如果您可以控制 XSLT 代码的功能,则可以让它在到达 jQuery 之前将响应列表包装在一组方括号中:

[{something: "foo", whatever:23}, {something: "bar", whatever, 32}]

如果您的 XSLT 生成了该代码,那么它(我希望)可以正常工作使用 getJSON 没问题。

编辑 好的,我现在看到你的问题了。

您的 JSON 响应在外部对象内包含多个“R”值。这是行不通的:如果“R”是一个列表,它需要有一个单个值,该值是一个数组。

  {"GSP": ..., "R":[{"U": ... }, {"U": ... }, {"U": ...}], ...}

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

{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.

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:

[{something: "foo", whatever:23}, {something: "bar", whatever, 32}]

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.

  {"GSP": ..., "R":[{"U": ... }, {"U": ... }, {"U": ...}], ...}
乙白 2024-09-09 03:45:12

或者,您始终可以只使用 $.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文