使用JavaScript重构JSON

时间:2015-10-19 20:08:17

标签: javascript jquery json

我正在学习JS而且我对我的工作有挑战但是我花了很多时间没有在这个JSON上获得正确的结构。所以这是我的问题:

主要的JSON就像:

{
    "Museum_Name":"Anchorage Museum",
    "Street_Address":"625 C St.",
    "City":"Anchorage",
    "State":"AK",
    "Full_State_Name":"Arkansas",
    "Zip_Code":99577,
    "Phone":"907-929-9201",
    "Website":"link",
    "Hours":"October to April:  Sat 10 a.m. to 6 p.m.; May to September: Sat 9 a.m. to  6 p.m.",
    "Provided_Description":"The Anchorage Museum is the largest museum in Alaska and one of the top 10 most visited attractions in the state. The museum’s mission is to share and connect Alaska with the world through art, history and science. Located in the heart of downtown Anchorage, the museum’s collection and exhibitions tell the story of Alaska’s past, present and future. Visitor favorites include Alaska Native artifacts, Alaska art and history galleries, a planetarium and a hands-on science center for families.",
    "Special_Note":"(No Sundays)",
    "Latitude_Longitude":"61.215906,-149.884839"
}

与很多城市,州和博物馆重复相同的结构,我使用了以下linq代码:

var query = $.Enumerable.From(data)
    .GroupBy(
        "{ Id: $.Full_State_Name}",
        "{ City_Name: $.City, Museum_Name: $.Museum_Name, link: $.Website}",
        "{ State: $.Id, Cities: $$.ToArray() }",
        "String($.Id) + $.City_Name + $.Website "
    )
    .ToJSON();

获得这个新的JSON:

[
    {
        "State": "California",
        "Cities": [
            {
                "City_Name": "Long Beach",
                "Museum_Name": "Museum of Latin American Art",
                "link": "link"
            },
            {
                "City_Name": "Los Angeles",
                "Museum_Name": "Skirball Cultural Center",
                "link": "link"
            },
            {
                "City_Name": "Los Angeles",
                "Museum_Name": "\nAutry National Center",
                "link": "link"
            }
        ]
    }
]

我需要以下结构,但我不知道如何获得它:

{
    "State": "California",
    "City": [
        {
            "City_Name": "Long Beach",
            "Museums": [
                {
                    "Museum_Name": "Museum of Latin American Art",
                    "link": "link"
                }
            ]
        },
        {
            "City_Name": "Los Angeles",
            "Museums": [
                {
                    "Museum_Name": "Skirball Cultural Center",
                    "link": "link"
                },
                {
                    "Museum_Name": "Autry National Center",
                    "link": "link"
                }
            ]
        }
    ]
}

我非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我能够将您的倒数第二个JSON块添加到最终的JSON块中。注意:JavaScript不是我选择的语言,因此鼓励进行编辑。让data成为具有多个状态的块,博物馆不会嵌套。

var new_data = [];
for (var state_index = 0; state_index < data.length; state_index++) {
    var state = data[state_index];
    var name = state.State;
    var cities = [];
    var city_index_map = {};

    for (var city_index = 0; city_index < state.Cities.length; city_index++) { 
        var city = state.Cities[city_index];   
        var museum = { "Museum_Name" : city.Museum_Name, "link" : city.link }

        var city_data = {};
        // City doesn't exist, so add it
        if (!(city.City_Name in city_index_map)) {
            city_index_map[city.City_Name] = city_index;
            city_data.City_Name = city.City_Name;
            city_data.Museums = [ museum ];
            cities.push(city_data);
        } else { // City already exists, so append the data
            var tmp_city_index = city_index_map[city.City_Name];
            cities[tmp_city_index].Museums.push(museum);
        }
    }
    new_data.push( {"State" : name, "Cities" : cities} );
}   

这是你的小提琴 - http://jsfiddle.net/g6p8pypj/1/