我有一个我想重新格式化的 JSON 对象,这就是我的意思......
JSON对象...
{
"query":"de mar",
"suggestions":[
"Any Location",
"Camp De Mar, Majorca, SPAIN",
"L'amettla De Mar, Costa Dorada, SPAIN",
"Lloret De Mar, Costa Brava, SPAIN",
"Malgrat De Mar, Costa Brava, SPAIN",
"Pineda De Mar, Costa Brava, SPAIN",
"Roquetas De Mar, Costa De Almeria, SPAIN",
"Tossa De Mar, Costa Brava, SPAIN"
],
"data":[
"",
"DestinationResort|Camp De Mar",
"DestinationResort|L'Amettla De Mar",
"DestinationResort|Lloret De Mar",
"DestinationResort|Malgrat De Mar",
"DestinationResort|Pineda De Mar",
"DestinationResort|Roquetas De Mar",
"DestinationResort|Tossa De Mar"
]
}
我想把它变成以下......
{
"destinations": [
"": "Any Location",
"DestinationResort|Camp De Mar": "Camp De Mar, Majorca, SPAIN",
// etc etc
]
}
目前我有以下(可怜地使用jquery编码)......
$("#search").on("input", function() {
$.getJSON( "http://holidays.allinclusive.co.uk/external/destinations.ashx?query=" + $("#search").val() )
.done(function(data) {
var myObject = ['destinations'];
$.each(data.suggestions, function (index, elem) {
myObject['destinations'] = {[data.data[index]] : [elem] };
});
})
});
我真正想做的是将它与 autocomplete() 或 bootstrap 的 typeahead() 一起使用来填充输入,但我认为这是我的下一步。
请问有人可以帮我吗?
谢谢