這裏是控制器用SpringMVC代碼段:爲什麼@ResponseBody將排序的LinkedHashMap返回爲未排序?
@RequestMapping(value = "/getCityList", method = RequestMethod.POST)
public @ResponseBody LinkedHashMap<String, String> getCityList(@RequestParam(value = "countryCode") String countryCode, HttpServletRequest request) throws Exception {
//gets ordered city list of country [sorted by city name]
LinkedHashMap<String, String> cityList = uiOperationsService.getCityList(countryCode);
for (String s : cityList.values()) {
System.out.println(s); //prints sorted list [sorted by name]
}
return cityList;
}
這裏是AJAX調用:
function fillCityList(countryCode) {
$.ajax({
type: "POST",
url: '/getCityList',
data: {countryCode:countryCode},
beforeSend:function(){
$('#city').html("<option value=''>-- SELECT --</option>");
}
}).done(function (data) {
console.log(data); // UNSORTED JSON STRING [Actually sorted by key... not by city name]
})
}
排序LinkedHashMap的從getCityList方法返回作爲未分類的JSON對象。爲什麼在退貨過程中訂單被更改? 由於ResponseBody註釋,LinkedHashMap是否轉換爲HashMap? 我可以通過Gson庫將我的排序對象轉換爲json字符串,並從我的getCityList方法返回json字符串,但我不喜歡這個解決方案。我能做些什麼來提供帶有排序列表的JavaScript回調方法?
我不喜歡我的方法列表,因爲我的jstl序列化使用LinkedHashMap:'