0
import org.json.JSONObject;

public class TestFile {

    public static void main(String d[]) throws Exception{

            String ch = "{'r':1,'is':'abc'}";

            JSONObject js  = new JSONObject(ch);

            System.out.println(js);

        }
}

When i run this program it the JSONObject prints {"is":"abc","r":1}, but i want in the same order of which the original string as it is.

please help me.

Thanks.

Regards
S.Chinna

2 Answers 2

2

Hash tables (JSON "objects") do not maintain key order. This is by design.

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

Comments

1

There is no guarantee that order is preserved, see Standard ECMA-262.

If you insist, try

{"data":{'r':1,'is':'abc'}, "ordering":['r','is']}

and iterate over ordering to lookup key in data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.