Merge two JSON objects programmatically

2019-01-24 13:59发布

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.

8条回答
Luminary・发光体
2楼-- · 2019-01-24 14:24

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

查看更多
在下西门庆
3楼-- · 2019-01-24 14:24

To make it more neat, you can add the merge function to the JSON object. This is the solution from Johan van de Merwe based on Elliot's answer, added to the actual JSON object.

// Extend JSON object
JSON.merge = function (o,ob) {

  for (var z in ob) {
    o[z] = ob[z];
  }

  return o;
}

json3 = JSON.merge(json1,json2);
查看更多
登录 后发表回答