以编程方式合并两个 JSON 对象

发布于 2024-07-29 00:07:52 字数 1050 浏览 3 评论 0原文

我这里有两个 JSON 对象,是通过 Google 搜索 API 生成的。 这些对象的 URL 可以在下面找到。

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

如您所见,第一个 URL 返回前 8 个结果,而第二个返回接下来的八个。 我不想单独检查这些结果,而是想以编程方式将它们合并到一个 JSON 对象中,并将它们作为前 16 个结果传递。

我已经尝试使用几个极其简单的 JSON 对象来实现这一点,但是 Google 返回的结果仍然有点超出我的想象,所以我希望在做这样的事情时得到一些帮助。

据我所知,将两个对象合并为一个对象并不违反 Google 的服务条款,只是这些对象总是作为两个结果进行处理(它们会这样做)。 一些朋友向我指出了能够完成此类任务的自动化工具的方向,但我还没有找到这样的工具。

我目前正在 ASP.NET 中工作,因此 C# 或 VB.NET 代码很棒,但我在某种程度上与语言无关,因此任何语言的任何帮助都将非常感激。

任何人都可以提供关于做这样的事情的任何帮助和/或建议吗?

编辑:这些结果最终将保存到数据库中,因此任何服务器端方法都很棒,即使这意味着将它们直接放入表中以便稍后处理。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

笔落惊风雨 2024-08-05 00:07:52
function MergeJSON (o, ob) {
      for (var z in ob) {
           o[z] = ob[z];
      }
      return o;
}

这看起来很像 Elliot 的代码,但在某些情况下更安全一些。 它不会向对象添加函数,这在与 Extjs 或 jQuery 等框架一起使用时可能会导致一些语法问题。 我遇到的问题是,当在事件侦听器中使用时,它给我带来了语法问题。 但功劳归于埃利奥特,他完成了这项工作。

使用如下:

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}

谢谢埃利奥特

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

死开点丶别碍眼 2024-08-05 00:07:52
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}
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}
雨轻弹 2024-08-05 00:07:52

我没有将两个结果合并在一起,而是决定解析它们,然后将这两个结果链接在一起。 最后,当它们可以轻松地连接到数据库中时,实际上没有必要将两者合并在一起。

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.

暖心男生 2024-08-05 00:07:52

使用 Jquery 你可以做到这一点!

a = $.extend({a:1}, {b:2});

result: Object { a=1,  b=2}

http://api.jquery.com/jQuery.extend/

With Jquery you could do this!

a = $.extend({a:1}, {b:2});

result: Object { a=1,  b=2}

http://api.jquery.com/jQuery.extend/

任性一次 2024-08-05 00:07:52

我不确定你如何完全合并这些东西,因为除了结果本身之外,每个东西都有很多额外的数据,但如果你只想要一个包含所有 16 个结果的 JavaScript 数组,这应该可行......

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);
}

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);
}
咽泪装欢 2024-08-05 00:07:52

为了使其更加简洁,您可以将 merge 函数添加到 JSON 对象。
这是 Johan van de Merwe 基于 Elliot 答案的解决方案,添加到实际的 JSON 对象中。

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

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

  return o;
}

json3 = JSON.merge(json1,json2);

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