2013-04-30 66 views
0

我想天气数据合并几个JSON文件,我已经和我试图找出做到这一点的最好办法。我发现其他几个职位,正好连接的JSON对象或有第二个覆盖第一个在同一个按键等,但没有通过键将它们合并的情况。合并JSON数据通过重点

下面就是我工作的样本,我想输出。任何建议,这将是伟大的!

样品JSON0.json

{ 
    "1948": [ 
     { 
      "Seattle": { 
       "city": "Seattle", 
       "data": { 
        "avg_dew_point": "34", 
        "avg_gust_wind": "", 
        "avg_max_temp": "45", 
        "avg_min_temp": "35", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.22", 
        "avg_temp": "40", 
        "avg_wind": "8" 
       }, 
       "month": "1", 
       "state": "WA" 
      } 
     }, 
     { 
      "Chicago": { 
       "city": "Chicago", 
       "data": { 
        "avg_dew_point": "10", 
        "avg_gust_wind": "", 
        "avg_max_temp": "25", 
        "avg_min_temp": "11", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.17", 
        "avg_temp": "18", 
        "avg_wind": "12" 
       }, 
       "month": "1", 
       "state": "IL" 
      } 
     } 
    ], 
    "1949": [ 
     { 
      "Houston": { 
       "city": "Houston", 
       "data": { 
        "avg_dew_point": "45", 
        "avg_gust_wind": "", 
        "avg_max_temp": "61", 
        "avg_min_temp": "44", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.15", 
        "avg_temp": "53", 
        "avg_wind": "12" 
       }, 
       "month": "1", 
       "state": "TX" 
      } 
     }, 
     { 
      "Seattle": { 
       "city": "Seattle", 
       "data": { 
        "avg_dew_point": "25", 
        "avg_gust_wind": "", 
        "avg_max_temp": "38", 
        "avg_min_temp": "25", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.32", 
        "avg_temp": "31", 
        "avg_wind": "7" 
       }, 
       "month": "1", 
       "state": "WA" 
      } 
     } 
    ] 
} 

样品JSON1.json

{ 
    "1948": [ 
     { 
      "Jacksonville": { 
       "city": "Jacksonville", 
       "data": { 
        "avg_dew_point": "45", 
        "avg_gust_wind": "", 
        "avg_max_temp": "61", 
        "avg_min_temp": "44", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.13", 
        "avg_temp": "53", 
        "avg_wind": "8" 
       }, 
       "month": "1", 
       "state": "FL" 
      } 
     }, 
     { 
      "Indianapolis": { 
       "city": "Indianapolis", 
       "data": { 
        "avg_dew_point": "13", 
        "avg_gust_wind": "", 
        "avg_max_temp": "34", 
        "avg_min_temp": "13", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.17", 
        "avg_temp": "24", 
        "avg_wind": "12" 
       }, 
       "month": "1", 
       "state": "IN" 
      } 
     } 
    ], 
    "1949": [ 
     { 
      "San Jose": { 
       "city": "San Jose", 
       "data": { 
        "avg_dew_point": "", 
        "avg_gust_wind": "", 
        "avg_max_temp": "52", 
        "avg_min_temp": "33", 
        "avg_precipitation": "0.02", 
        "avg_sea_level_pressure": "", 
        "avg_temp": "43", 
        "avg_wind": "" 
       }, 
       "month": "1", 
       "state": "CA" 
      } 
     }, 
     { 
      "Jacksonville": { 
       "city": "Jacksonville", 
       "data": { 
        "avg_dew_point": "55", 
        "avg_gust_wind": "", 
        "avg_max_temp": "73", 
        "avg_min_temp": "54", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.21", 
        "avg_temp": "63", 
        "avg_wind": "5" 
       }, 
       "month": "1", 
       "state": "FL" 
      } 
     } 
    ] 
} 

样品combinedJSON.json

{ 
    "1948": [ 
     { 
      "Seattle": { 
       "city": "Seattle", 
       "data": { 
        "avg_dew_point": "34", 
        "avg_gust_wind": "", 
        "avg_max_temp": "45", 
        "avg_min_temp": "35", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.22", 
        "avg_temp": "40", 
        "avg_wind": "8" 
       }, 
       "month": "1", 
       "state": "WA" 
      } 
     }, 
     { 
      "Chicago": { 
       "city": "Chicago", 
       "data": { 
        "avg_dew_point": "10", 
        "avg_gust_wind": "", 
        "avg_max_temp": "25", 
        "avg_min_temp": "11", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.17", 
        "avg_temp": "18", 
        "avg_wind": "12" 
       }, 
       "month": "1", 
       "state": "IL" 
      } 
     }, 
     { 
      "Jacksonville": { 
       "city": "Jacksonville", 
       "data": { 
        "avg_dew_point": "45", 
        "avg_gust_wind": "", 
        "avg_max_temp": "61", 
        "avg_min_temp": "44", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.13", 
        "avg_temp": "53", 
        "avg_wind": "8" 
       }, 
       "month": "1", 
       "state": "FL" 
      } 
     }, 
     { 
      "Indianapolis": { 
       "city": "Indianapolis", 
       "data": { 
        "avg_dew_point": "13", 
        "avg_gust_wind": "", 
        "avg_max_temp": "34", 
        "avg_min_temp": "13", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.17", 
        "avg_temp": "24", 
        "avg_wind": "12" 
       }, 
       "month": "1", 
       "state": "IN" 
      } 
     } 
    ], 
    "1949": [ 
     { 
      "Houston": { 
       "city": "Houston", 
       "data": { 
        "avg_dew_point": "45", 
        "avg_gust_wind": "", 
        "avg_max_temp": "61", 
        "avg_min_temp": "44", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.15", 
        "avg_temp": "53", 
        "avg_wind": "12" 
       }, 
       "month": "1", 
       "state": "TX" 
      } 
     }, 
     { 
      "Seattle": { 
       "city": "Seattle", 
       "data": { 
        "avg_dew_point": "25", 
        "avg_gust_wind": "", 
        "avg_max_temp": "38", 
        "avg_min_temp": "25", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.32", 
        "avg_temp": "31", 
        "avg_wind": "7" 
       }, 
       "month": "1", 
       "state": "WA" 
      } 
     }, 
     { 
      "San Jose": { 
       "city": "San Jose", 
       "data": { 
        "avg_dew_point": "", 
        "avg_gust_wind": "", 
        "avg_max_temp": "52", 
        "avg_min_temp": "33", 
        "avg_precipitation": "0.02", 
        "avg_sea_level_pressure": "", 
        "avg_temp": "43", 
        "avg_wind": "" 
       }, 
       "month": "1", 
       "state": "CA" 
      } 
     }, 
     { 
      "Jacksonville": { 
       "city": "Jacksonville", 
       "data": { 
        "avg_dew_point": "55", 
        "avg_gust_wind": "", 
        "avg_max_temp": "73", 
        "avg_min_temp": "54", 
        "avg_precipitation": "0.00", 
        "avg_sea_level_pressure": "30.21", 
        "avg_temp": "63", 
        "avg_wind": "5" 
       }, 
       "month": "1", 
       "state": "FL" 
      } 
     } 
    ] 
} 
+0

遍历对象(http://stackoverflow.com/q/85992/218196)和合并的阵列(http://stackoverflow.com/q/1584370/218196)。 – 2013-04-30 14:47:44

+0

有你看着小工具JS库之一,如下划线或Lodash:http://underscorejs.org/ http://lodash.com/docs他们有很多的采集处理功能 – djKianoosh 2013-04-30 14:50:51

+0

你可能considor步入jQuery的延伸,获取jQuery的未经版本化的版本,编写代码如$ .extend(true,{a:“a”},{b:“b”});按F12并在那里设置断点,加载页面并进入代码。涉及到相当多的代码,但它在深度复制对象方面做得很好,涉及不包含数组或函数的对象的函数是:extend,isPlainObject和type。 – HMR 2013-04-30 15:11:27

回答

2

试试这个:

function mergeJson(target) { 
    for (var argi = 1; argi < arguments.length; argi++) { 
     var source = arguments[argi]; 
     for (var key in source) { 
      if (!(key in target)) { 
       target[key] = []; 
      } 
      for (var i = 0; i < source[key].length; i++) { 
       target[key].push(source[key][i]); 
      } 
     } 
    } 
    return target; 
} 

DEMO:http://jsfiddle.net/4nfWp/1/

而且你怎么称呼它,如:mergeJson({}, object1, object2, object3);(与任意数量的参数过去1号)

在演示中,实际调用是在JavaScript的底部,因为我需要的首先定义“JSON”,并且它们很长。

UPDATE:

如果使用underscorejs,您可以使用类似:

var finalJson = {}; 
_.each(_.keys(json0,json1), function(key) { 
    finalJson[key] = _.flatten(_.zip(json0[key], json1[key])); 
}); 
console.log(finalJson); 

DEMO:http://jsfiddle.net/4nfWp/4/

幸得@djKianoosh为搞清楚了这一点: )

+0

我调整了的jsfiddle使用下划线的ZIP /个/扁平化的功能,看看它是否会工作使用这些:http://jsfiddle.net/4nfWp/3/ – djKianoosh 2013-04-30 15:14:32

+0

@djKianoosh非常酷,感谢工作就可以了,并想出来的。我把它放在一起,并更新我的答案,包括小提琴,并给你信用:)我从来没有与'underscorejs'工作,这样的解决方案永远不会跨过我的脑海。与此同时,你显然也欢迎自己做出答案,因为它有效 – Ian 2013-04-30 15:20:56