合并两个JSON对象编程 [英] Merge two JSON objects programmatically

查看:141
本文介绍了合并两个JSON对象编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个JSON对象在这里,通过谷歌搜索API生成的。这些对象的网址可以在下面找到。

<一个href=\"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
<一href=\"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=large&start=8\">http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=large&start=8

正如你所看到的第一个URL返回前八的结果,而第二个返回下一个八强。相反,这些检查结果另行我想的编程的它们合并成一个JSON对象,并通过将它们作为前16个结果。

我和几个非常简单的JSON对象的尝试这一点,但什么谷歌返回仍然是我的头有点上面,所以我希望能有点用做这样的事的帮助。

至于有人告诉我这是不是对谷歌的服务条款,以两个对象合并为一个,只有这些总是要经过两个结果(他们会)。有朋友曾经指出我的自动化工具,能够做这样的事情的方向,但我还没有找到这样的工具。

我目前在ASP.NET中工作,所以C#或VB.NET code是伟大的,但我有点独立的语言,以便在任何语言任何帮助将是非常美联社preciated。

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

编辑:这些结果最终将被保存到数据库中,所以任何服务器端方法将会是非常美妙的,即使这意味着将他们直接到表处理后


解决方案

 函数MergeJSON(O,OB){
      对(在OB变种Z){
           Ø[Z] = OB [Z]。
      }
      返回O;
}

这看起来很像从艾略特的code,但在某些时候是有点更安全。它不加入一个功能的对象,这可能导致一些语法问题,像EXTJS或jQuery的一个框架中使用的时候。我曾在一个事件监听使用时,它给了我在语法问题的问题。但信贷去埃利奥特,他做的工作。

使用此如下:

  A = {A:1}
B = {B:2}
C = {C:3}X = MergeJSON(A,B);
X = MergeJSON(X,C);结果:X == {A:1,B 2,C:3}

感谢您艾略特

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.

解决方案

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

这篇关于合并两个JSON对象编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆