0

我正在尝试合并我拥有的几个关于天气数据的 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"
            }
        }
    ]
}

示例组合JSON.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"
            }
        }
    ]
}
4

1 回答 1

3

试试这个:

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;
}

演示:http: //jsfiddle.net/4nfWp/1/

你这样称呼它:(mergeJson({}, object1, object2, object3);在第一个之后有任意数量的参数)

在演示中,实际调用位于 Javascript 的底部,因为我需要先定义“JSON”,而且它们很长。

更新:

如果使用underscorejs,你可以使用类似的东西:

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

演示:http: //jsfiddle.net/4nfWp/4/

感谢@djKianoosh解决了这个问题:)

于 2013-04-30T14:54:42.073 回答