0

I am facing a very weird issue(at least that's what I think). I have an API which returns an object in JSON format. There is a linkedHashMap field in this object. This map is showing some weird behaviour. When I am logging this object just before sending it to the client it has data in the format I want. But on the client side the map has the keys in different order compare to the logged one. Below is code :

@GET
@Produces(MediaType.APPLICATION_JSON)
public BookingHistoryResponse getBookingHistory(
        @QueryParam("from") int fromId,
        ){

    BookingHistoryRequest request = new BookingHistoryRequest(fromId);
    BookingHistoryResponse response = new BookingHistoryResponse();
    bookingHistoryService.execute(request, response);
    logger.info("before returning the repsonse..");
    logger.info(response);
    Iterator it = response.getSeatDetailsMap().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        logger.info(pair.getKey() + " = " + pair.getValue());
    }
    return response;
}

The response of the above code is in correct format. But the response at the client side is totally different than what is getting logged. Since there is no further manipulation done on this object, hence I am not able to find out why the map is giving me different result compare to the logged result. Any insight would be highly appreciated. Thanks in advance.

viks1010
  • 101
  • 8
  • I would guess the algorithm that converts the map to json does not take the order into account (no indices?). I would convert the map to a list before sending the response. – lisa p. Feb 11 '16 at 11:15
  • Check this: http://stackoverflow.com/questions/17229418/jsonobject-why-jsonobject-changing-the-order-of-attributes if you don't have an indexed list You can't order json attributes. – lisa p. Feb 11 '16 at 11:16
  • 1
    Thanks for the quick response. I did not know that order of the keys of the map would also get rearranged(even when I declare it as linkedHashMap). I t would be tough to achieve what I want without a map (i.e cant use List). – viks1010 Feb 11 '16 at 11:29
  • The json format just doesn't offer the functionality of a LinkedHashMap. Maybe the other questions on this topic offer an answer that works for you. – lisa p. Feb 11 '16 at 12:54

0 Answers0