合并JSON结构 [英] merge JSON structures
问题描述
是否有某个功能或库合并了2个JSON数据
结构?例如:
{" one":[1,3]}
plus
{" one":[2]}
等于
{" one":[1,2,3 }}
11月13日,05:43,hobosales。 .. @ gmail.com写道:
是否有某个函数或库合并了2个JSON数据
结构?例如:
{" one":[1,3]}
plus
{" one":[2]}
等于
{" one":[1,2,3 ]}
这是一个非常特殊的情况。在原型库中,您可以找到
Object.extend(),但它会覆盖相同的键。你需要一个特殊的
函数来汇总密钥数组(如果密钥包含一个数组),
例如
对象.plus = function(obj1,obj2){
var res = {};
//" copy" obj1键到新对象res
Object.extend(res,obj1);
//现在用obj2的内容覆盖res中的obj1键
Object.extend(res,obj2);
//现在考虑逐个覆盖obj 1个键,就像在示例中一样 -
only arrays
for(obj1中的var键){
if(obj1 [key] instanceof Array)
//连接数组,不要像你的例子那样排序
res [key] = obj1 [key] .concat(res [key]);
}
}
我希望我可以帮助你,对于Object.extend来看看
http://script.aculo.us/prototype.js
Andi
< blockquote>并返回结果:
Object.plus = function(obj1,obj2){
...
返回res;
}
ho ********** @ gmail.com 写道:
是否有某个函数或库合并了2个JSON数据
结构?例如:
{" one":[1,3]}
plus
{" one":[2]}
等于
{" one":[1,2,3 ]}
如果您具体使用上述结构,那么以下
应该可以解决这个问题:
>
< script type =" text / javascript" src =" json.js">< / script>
< script type =" text / javascript">
var jsonStr1 =''{" one":[1,3]}'';
var jsonStr2 =''{" one":[2],''
+''" two":[1]}'';
函数jsonAdd(str1,str2){
var obj1 = eval(' '(''+ str1 +'')'');
var obj2 = eval(''(''+ str2 +'')'');
for(var prop in obj2){
if(obj2 [prop] instanceof Array){
if(prop in obj1){
obj1 [prop] = obj1 [prop] .concat(obj2 [prop]);
} else {
obj1 [prop] = obj2 [prop] .concat();
}
}
}
返回obj1.toJSONString();
}
alert(jsonAdd(jsonStr1,jsonStr2));
< / script>
其中json.js可以在:< URL: http://www.json.org/json.js >。如果
你不想扩展Array原型(因此可以
删除instanceof Array测试),有一个功能化的版本
$ b json.org已不再提供$ b - 已在此处发布:
< URL:
http://groups.google.com。 au / group / ru ... 43fb9e0b2f5a3d
>
-
Rob
Is there a function or library somewhere that merges 2 JSON data
structures? For instance this:
{ "one": [1,3] }
plus
{ "one": [2] }
equals
{ "one": [1,2,3] }
On 13 Nov., 05:43, hobosales...@gmail.com wrote:Is there a function or library somewhere that merges 2 JSON data
structures? For instance this:
{ "one": [1,3] }
plus
{ "one": [2] }
equals
{ "one": [1,2,3] }This is a very special case. In prototype library you find
Object.extend(), but it overwrites same keys. You need a special
funcion that concats Arrays key for key (if the key holds an array),
e.g.
Object.plus = function(obj1, obj2) {
var res = {};
// "copy" obj1 keys to new object res
Object.extend(res, obj1);
// now overwrite obj1 keys in res by contents of obj2
Object.extend(res, obj2);
// now consider overwritten obj 1 keys one by one, like in example -
only arrays
for(var key in obj1) {
if (obj1[key] instanceof Array)
// concatenate Arrays, don''t sort them like in your example
res[key] = obj1[key].concat(res[key]);
}
}
I hope I could help you, for Object.extend look in
http://script.aculo.us/prototype.js
Andi
And return the result:
Object.plus = function(obj1, obj2) {
...
return res;
}
ho**********@gmail.com wrote:Is there a function or library somewhere that merges 2 JSON data
structures? For instance this:
{ "one": [1,3] }
plus
{ "one": [2] }
equals
{ "one": [1,2,3] }If you are specifically after the above structure, then the following
should do the trick:
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript">
var jsonStr1 = ''{ "one": [1,3] }'';
var jsonStr2 = ''{ "one": [2],''
+ '' "two": [1]}'';
function jsonAdd(str1, str2){
var obj1 = eval(''('' + str1 + '')'');
var obj2 = eval(''('' + str2 + '')'');
for (var prop in obj2){
if (obj2[prop] instanceof Array){
if (prop in obj1){
obj1[prop] = obj1[prop].concat(obj2[prop]);
} else {
obj1[prop] = obj2[prop].concat();
}
}
}
return obj1.toJSONString();
}
alert( jsonAdd(jsonStr1, jsonStr2) );
</script>
where json.js can be found at: <URL: http://www.json.org/json.js >. If
you don''t want to extend the Array prototype (and therefore could
remove the instanceof Array test), there is a functionalised version
that is no longer available from json.org - it has been posted here:
<URL:
http://groups.google.com.au/group/ru...43fb9e0b2f5a3d>
--
Rob
这篇关于合并JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!