我想天氣數據合併幾個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"
}
}
]
}
遍歷對象(http://stackoverflow.com/q/85992/218196)和合並的陣列(http://stackoverflow.com/q/1584370/218196)。 – 2013-04-30 14:47:44
有你看着小工具JS庫之一,如下劃線或Lodash:http://underscorejs.org/ http://lodash.com/docs他們有很多的採集處理功能 – djKianoosh 2013-04-30 14:50:51
你可能considor步入jQuery的延伸,獲取jQuery的未經版本化的版本,編寫代碼如$ .extend(true,{a:「a」},{b:「b」});按F12並在那裏設置斷點,加載頁面並進入代碼。涉及到相當多的代碼,但它在深度複製對象方面做得很好,涉及不包含數組或函數的對象的函數是:extend,isPlainObject和type。 – HMR 2013-04-30 15:11:27