这里是控制器用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:'