【发布时间】:2014-11-22 23:41:48
【问题描述】:
这里是SpringMVC控制器代码sn-p:
@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 对象。为什么在退货过程中更改订单? LinkedHashMap 是否因为 ResponseBody 注解而转换为 HashMap? 我可以通过 Gson 库将排序的对象转换为 json 字符串,并从我的 getCityList 方法返回 json 字符串,但我不喜欢这个解决方案。我可以做些什么来提供带有排序列表的javascript回调方法?
【问题讨论】:
标签: java json sorting spring-mvc linkedhashmap