-2

I have json that I want to parse as a JSONObject. When I do so, it messes up the order of the json, see below. How can I fix this?

original json:

{"result":
    {"headers":
         {
         "month_1":"May 2013",
         "month_2":"April 2013",
         "month_3":"March 2013",
         "month_4":"February 2013",
         "month_5":"January 2013",
         "month_6":"December 2012",
         "month_7":"November 2012"
          }
     }
}

After parsing to JSONObject:

{"result":
    {"headers":
        {
        "month_6":"December 2012",
        "month_5":"January 2013",
        "month_4":"February 2013",
        "month_3":"March 2013",
        "month_2":"April 2013",
        "month_1":"May 2013",
        "month_7":"November 2012"
        }
    }
}

My code:

private void ProcessResponse(String response) {
    JSONObject json = new JSONObject(response);
}
TPeer
  • 117
  • 8

2 Answers2

0

JSON libraries does stick to the order of elemts within JSON obects. To get more details please follow JSON.org. But the code should not be tightly dependent on the order of elements within a json object.

Madhusudan Joshi
  • 4,438
  • 3
  • 26
  • 42
0

A way I can think of is, try to convert JSONObject to an arraylist, then use the code below. All I can think of at the moment. How to convert, try to search this on google.

 Collections.sort("your arraylist", new Comparator<Item>() {
            public int compare(Item i1, Item i2) {
                return i1.getCaption().compareTo(i2.getCaption());
            }
        });
Bigflow
  • 3,616
  • 5
  • 29
  • 52