0

I have JSON Object (Java), which is some key value pairs. My main objective is to use JSONObject.toString() and get the result in sorted orders w.r.t Keys.

Is there anyway by which I can achieve this.

jsonObject.put("z","A");
jsonObject.put("x", "B");
jsonObject.put("n", "C");
jsonObject.put("t", "D");`

Expected Output of jsonObject.toString

{"n":"C","t":"D","x":"B","z":"A"}
5
  • Possible duplicate of How can i sort my JSON object based on Key? Commented Mar 6, 2019 at 13:30
  • And this Commented Mar 6, 2019 at 13:31
  • @FedericoklezCulloca In this case I have doubt on solution of Christian, as I need output of JSONObject.toString in sorted order. Commented Mar 6, 2019 at 13:34
  • Yes, I misread your question before you edited it. The answer to the second link seems more in line with what you want. Commented Mar 6, 2019 at 13:36
  • @FedericoklezCulloca Thanks for the Second Solution, its working perfectly fine. Commented Mar 6, 2019 at 13:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.