How to maintain the order of a JSONObject

java, json

Solution

You can't.

That is why we call it an unordered collection of name/value pairs.

Why you would need to do this, I'm not sure. But if you want ordering, you'll have to use a json array.

Problem

I am using a JSONObject in order to remove a certin attribute I don't need in a JSON String: ``` JSONObject jsonObject = new JSONObject(jsonString); jsonObject.remove("owner"); jsonString = jsonObject.toString(); ``` It works ok however the problem is that the JSONObject is "an unordered collection of name/value pairs" and I want to maintain the original order the String had before it went through the JSONObject manipulation. Any idea how to do this?

Original source

Related problems