2013-04-30 55 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