如何在javascript / jquery中合并两个json对象?

时间:2013-01-13 21:05:23

标签: javascript jquery json

我有一个如下所示的数据集:

{
"Stops": [
    {
        "Name": "COLUMBIA RD NW + MINTWOOD PL NW",
        "Routes": [
            "42",
            "43",
            "H1",
            "L2"
        ]
    }
]

}

另一个看起来像这样:

{
"Predictions": [
    {
        "DirectionText": "North to Mt Pleasant Via Adams Morgan",
        "Minutes": 7,
        "RouteID": "42"
    },
    {
        "DirectionText": "North to Mt Pleasant Via Adams Morgan",
        "Minutes": 25,
        "RouteID": "42"
    },
    {
        "DirectionText": "North to Chevy Chase Circle",
        "Minutes": 32,
        "RouteID": "L2"
    },
    {
        "DirectionText": "North to Mt Pleasant Via Adams Morgan",
        "Minutes": 36,
        "RouteID": "42"
    },
    {
        "DirectionText": "North to Mt Pleasant Via Adams Morgan",
        "Minutes": 58,
        "RouteID": "42"
    },
    {
        "DirectionText": "North to Mt Pleasant Via Adams Morgan",
        "Minutes": 69,
        "RouteID": "42",
    }
],
"StopName": "Columbia Rd Nw + Mintwood Pl Nw"

}

我想在第一组中合并Route ID上的数据,以特别添加要设置的分钟数。所以输出将是这样的:

{
"Stops": [
    {
        "Name": "COLUMBIA RD NW + MINTWOOD PL NW",
        "Routes": [
            {
                "42": [
                    {
                        "Minutes": [7, 25,36,58,69]
                    }
                ],
                "43": [
                    {
                        "Minutes": []
                    }
                ],
                "H1":[ 
                    {
                        "Minutes": []
                    }
                ],
                "L2": [
                    {
                        "Minutes": [32]
                    }
                ]
            }
        ],
        "StopID": "1001779"
    }
]
}

我一直坚持如何编写一些将进行此合并的javascript / jquery。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

请,这是更新的代码,包含空数组和所有内容。它是普通的JavaScript,因此不需要库。 它是我所知道的一个粗糙的例子,但是它有效,我希望它能帮助你找到自己的方式。后来你可以改进它。

var 

    stops = {
        "Stops": [
            {
                "Lat": 38.92092,
                "Lon": -77.043684,
                "Name": "COLUMBIA RD NW + MINTWOOD PL NW",
                "Routes": [
                    "42",
                    "43",
                    "H1",
                    "L2"
                ],
                "StopID": "1001779"
            }
        ]
    },

    predictions = {
        "Predictions": [
            {
                "DirectionNum": "1",
                "DirectionText": "North to Mt Pleasant Via Adams Morgan",
                "Minutes": 7,
                "RouteID": "42",
                "VehicleID": "7136"
            },
            {
                "DirectionNum": "1",
                "DirectionText": "North to Mt Pleasant Via Adams Morgan",
                "Minutes": 25,
                "RouteID": "42",
                "VehicleID": "7103"
            },
            {
                "DirectionNum": "0",
                "DirectionText": "North to Chevy Chase Circle",
                "Minutes": 32,
                "RouteID": "L2",
                "VehicleID": "2207"
            },
            {
                "DirectionNum": "1",
                "DirectionText": "North to Mt Pleasant Via Adams Morgan",
                "Minutes": 36,
                "RouteID": "42",
                "VehicleID": "7132"
            },
            {
                "DirectionNum": "1",
                "DirectionText": "North to Mt Pleasant Via Adams Morgan",
                "Minutes": 58,
                "RouteID": "42",
                "VehicleID": "7111"
            },
            {
                "DirectionNum": "1",
                "DirectionText": "North to Mt Pleasant Via Adams Morgan",
                "Minutes": 69,
                "RouteID": "42",
                "VehicleID": "7135"
            }
        ],
        "StopName": "Columbia Rd Nw + Mintwood Pl Nw"
    };

    var routeTimes = {};
    for (var index in predictions.Predictions) {
        if(!routeTimes.hasOwnProperty(predictions.Predictions[index].RouteID)) {
            routeTimes[predictions.Predictions[index].RouteID] = [];
            routeTimes[predictions.Predictions[index].RouteID].push(predictions.Predictions[index].Minutes);
        } else {
            routeTimes[predictions.Predictions[index].RouteID].push(predictions.Predictions[index].Minutes);
        }
    }

    var 
        routes = stops.Stops[0].Routes,
        routesVsMinutes = {};

    for(var i in routes) {
        if (!routesVsMinutes.hasOwnProperty(routes[i])) {
            routesVsMinutes[routes[i]] = {Minutes: []};
        } 
        if (routeTimes[routes[i]]) {
            routesVsMinutes[routes[i]].Minutes = routeTimes[routes[i]];
        }
    }      
    stops.Stops[0].Routes = routesVsMinutes;

    console.log(stops);

答案 1 :(得分:0)

您可以使用jQuery map

json1.Stops.routes = $.map(json1.Stops[0].Routes, function(route){
  //Take all predictions which concern the route
  var availables = $.filter(json2.Predictions, function(prediction){
    return prediction.RouteID === route
  })
  //Extract all minutes
  var minutesArray = $.map(availables, function(available){
    return available.Minutes
  })
  // Return the object to put in "routes"
  return {"Minutes": minutesArray}
})

使用:

json1 = {"Stops":[...]}
json2 = {"Predictions:[...]}
相关问题