views:

2007

answers:

7

I have two JSON objects here, generated through the Google Search API. The URL's of these objects can be found below.

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=large http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=large&start=8

As you can see the first URL returns the first eight results, whilst the second one returns the next eight. Instead of checking these results separately I'd like to programmatically merge them into one JSON object and pass them through as the first sixteen results.

I've attempted this with a couple of extremely simple JSON objects, but what Google returns is still a bit above my head, so I'm hoping for a bit of help with doing such a thing.

As far as I've been told it is not against Google's Terms of Service to merge two objects into one, only that these always go through as two results (which they will). Some friends have pointed me in the direction of automated tools that are capable of doing such things, but I'm yet to find such a tool.

I'm currently working within ASP.NET so C# or VB.NET code is great, but I'm somewhat language independent so any help in any language will be very much appreciated.

Can anyone provide any help and/or advice on doing such a thing?

EDIT: These results will eventually be saved to a database, so any server-side methods would be fantastic, even if it means putting them straight into a table for dealing with later.

A: 

I'm not sure how you'd merge these things completely, given that there's a lot of extra data in each apart from the results themselves, but if you just want a JavaScript array containing all 16 results, this should work...

var responses = [GetJsonObjectFromThatUriUsingJqueryOrSomething(),
                 GetJsonObjectFromThatOtherUriUsingJqueryOrSomething()];

// Probably want to check for success
// and ensure that responses[i].responseData.results is valid.

var results = [];
for (var i = 0; i < responses.length; ++i)
{
    results = results.concat(responses[i].responseData.results);
}
Domenic
The post is asking for a .NET solution, not a JavaScript solution... I did the same mistake, but I think it's best to delete the JavaScript solutions to help de-clutter the question.
Blixt
+1  A: 

Also, if you really want to do the results manipulation server-sided, this article seems to give a pretty reasonable walkthrough of the process.

Domenic
I've read through this approach already, and whilst it seems simple enough with simple JSON objects I've struggled with Google's crazy output. My overall goal is to show all twelve search results and input them into a database, so it'd be great if they could be grabbed individually and added to variables within an ASP.NET page. I'll give this method another try soon and post my results.
EnderMB
A: 

If you are up to a client side solution(JavaScript actually) what about trying the "unite" function I have written: http://jslib-test.amplio-vita.net/JSLib/js/aV.ext.object.js

BYK
A: 

Rather than merge the two results together, I just decided to parse them, then link those two together. In the end there was really no need to merge the two together when they could be easily joined within a database.

EnderMB
+3  A: 

Object.prototype.merge = (function (ob) {var o = this;var i = 0;for (var z in ob) {if (ob.hasOwnProperty(z)) {o[z] = ob[z];}}return o;})

var a = {a:1} var b = {b:2}

var c = a.merge(b); // === {a:1,b:2}

elliot
+3  A: 

function MergeJSON (o, ob) { for (var z in ob) { o[z] = ob[z]; } return o; }

This looks a lot like the code from Elliot, but is a bit safer in some conditions. It is not adding a function to the object, which could lead to some syntax problems, when used in with a framework like Extjs or jQuery. I had the problem that it gave me problems in the syntax when used in an event listener. But credits go to Elliot, he did the job.

Use this as following:

a = {a : 1} b = {b : 2} c = {c : 3}

x = MergeJSON ( a, b); x = MergeJSON ( x, c);

result : x == {a : 1, b : 2, c : 3}

Thank you Elliot

Johan van de Merwe