3

Here is SpringMVC Controller code snippet:

@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; } 

Here is ajax call:

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] }) } 

Sorted LinkedHashMap returns as unsorted JSON object from getCityList method. Why order is changed during return process ? Is LinkedHashMap converted to HashMap because of ResponseBody annotation? I can convert my sorted object to json string via Gson library and return json string from my getCityList method but i don't like this solution. What can i do to provide javascript callback method with sorted list?

2 Answers 2

8

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<City> from your method, where City has a key and a value.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't prefer List for my methods because my jstl serialization uses LinkedHashMap: <form:options items="${cityList}"></form:options>. But I think unwillingly i should make exception and convert LinkedHashMap to List in methods that returns response to ajax calls.
0

If you wanted to send sorted data then pass List of Your object or City as List<City> which have sorted data.

I have also face same problem when I have used HashMap in my application, it doesn't guarantee that it will receive in same order in which we add them. We have to access it via it's key.

so it's better to use List instead of LinkedHashMap.

Most implementations of JSON make no effort to preserve the order of an object's name/value pairs, since it is (by definition) not significant.

7 Comments

Are you sure about LinkedHasMap insertion order ? Java docs suggests otherwise docs.oracle.com/javase/7/docs/api/java/util/LinkedHashMap.html
@Shailendra : see java.dzone.com/articles/… with example.
What do you mean by implementations of JSON? JSON is a format.
Yes it is a format, I am just talking about that the order is not preserve in the JSON when we use it. look stackoverflow.com/questions/4515676/…
@user3145373 : the article you mentions too states "LinkedHashMap preserves the insertion order" just like what javadocs states
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.