Why @ResponseBody returns sorted LinkedHashMap as not sorted?

后端 未结 2 680
野性不改
野性不改 2021-01-20 06:30

Here is SpringMVC Controller code snippet:

@RequestMapping(value = \"/getCityList\", method = RequestMethod.POST)
public @ResponseBody LinkedHashMap

        
2条回答
  •  悲哀的现实
    2021-01-20 07:02

    You're expecting a JSON object's entries to have the same order as the LinkedHashMap entries. That won't happen, because JavaScript object keys have no intrinsic order. They're just like Java HashMaps.

    If you need a JavaScript data structure that maintains an order, you should use an array, not an object. Return a sorted List from your method, where City has a key and a value.

提交回复
热议问题