Your code is incorrect if you're relying on ordering. I'll get to that, but first some background which may come in handy:
HashMaps are unordered. If you want to preserve order, there's LinkedHashMap, which retains the order, so when you iterate over the map, you get the order in which they were inserted. And there's TreeMap, which will enforce a sorting order for you, independent of what order they're supplied in. Unfortunately, that doesn't help you, since the JSONObject implementation uses HashMap. But you can iterate over the keys you get back with JSONObject.keys(), and store the keys in a TreeSet, sorted according to your preferences, or in an ArrayList, and call Collections.sort() on it, and then iterate over the result. That's a simple, robust solution, if the client knows what order the keys should be in. But that doesn't give you the original order. The options I see there are: * A modified version of the JSON code. I don't recommend it, especially not to deviate from the standard, but if you have no control over what you're being sent, this may be your only alternative. Just take the current code, and whenever it does 'new HashTable()', do 'new LinkedHashTable()'. * Modify your protocol to pass the desired key order explicitly (as the value of another key on the object, or on some single larger object, etc.). Call JSONObject.keys(), and iterate over that. This is the most robust solution, but requires server-side cooperation, and increases the payload size a bit. I may be slightly misremembering, but if I recall correctly, preservation of the key ordering was NOT part of the Javascript/ ECMAscript standards -- but was so widely implemented and relied on that in the most recent round they made it part of the standard-to-be. HOWEVER, JSON is *NOT* Javascript, it is a data interchange language. >From the JSON web site: "An object is an unordered set of name/value pairs" So this is not a JSON bug; it is behaving as expected, for compatibility across a wide range of languages. So you'll either have to change your protocol, or accommodate your deviation from the standard. On Feb 9, 7:49 pm, Kasra Rahjerdi <johncena4presid...@gmail.com> wrote: > Hello, > I am currently turning a valid-JSON expression from a web page into a > JSONObject using org.JSON. This object has a really weird order. Each > time my expression changes the order of the strings in the JSONObject > change too. Right now, the debugger tells me that the first two > elements in the JSONObject's hashmap are null, and I am seeing the > third item in my JSON expression as the first item in my view. > > All my items still show up in my ListView, they just all show up in > weird ways. > > Is this a common issue? Is there any way to sort my JSONObject? (My > keys are all numbers in descending order). > > Thank You, > Kasra -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en