Reputation: 1572
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.
Upvotes: 3
Views: 1430
Reputation: 651
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.
Upvotes: 0
Reputation: 413836
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": ...}], ...}
Upvotes: 2