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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1198962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:41:09  来源:igfitidea点击:

Merge two JSON objects programmatically

c#asp.netjsonstring-concatenation

提问by Mike B

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

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

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=largehttp://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 http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q =hello%20world&rsz=大&开始=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 programmaticallymerge them into one JSON object and pass them through as the first sixteen results.

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

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.

我已经用几个非常简单的 JSON 对象尝试了这个,但是谷歌返回的东西仍然有点超出我的想法,所以我希望在做这样的事情时得到一些帮助。

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.

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

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.

我目前在 ASP.NET 中工作,所以 C# 或 VB.NET 代码很棒,但我有点独立于语言,因此非常感谢任何语言的任何帮助。

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.

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

采纳答案by Johan van de Merwe

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.

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

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

谢谢艾略特

回答by Domenic

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...

我不确定你会如何完全合并这些东西,因为除了结果本身之外还有很多额外的数据,但是如果你只想要一个包含所有 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);
}

回答by Domenic

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

此外,如果您真的想在服务器端进行结果操作,本文似乎提供了一个非常合理的流程演练。

回答by BYK

If you are up to a client side solution(JavaScript actually) what about trying the "unite" function I have written: http://code.google.com/p/av-jslib/source/browse/js/aV.ext.object.js#36

如果您使用客户端解决方案(实际上是 JavaScript)如何尝试我编写的“联合”功能:http: //code.google.com/p/av-jslib/source/browse/js/aV.ext .object.js#36

回答by Mike B

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.

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

回答by elliot

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}

回答by Diego Gallardo

With Jquery you could do this!

使用 Jquery,您可以做到这一点!

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

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

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

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

回答by Max Kielland

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

为了使它更整洁,您可以将该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);