5

I am creating a JSON object in which i am adding a key and a value which is an array. The value for both key and value comes from a TreeSet which has data in sorted form. However, when I insert data in my json object, it is stored randomly without any order. This is my json object currently:

{
    "SPAIN":["SPAIN","this"],
    "TAIWAN":["TAIWAN","this"],
    "NORWAY":["NORWAY","this"],
    "LATIN_AMERICA":["LATIN_AMERICA","this"]
}

and my code is:

 Iterator<String> it= MyTreeSet.iterator();

        while (it.hasNext()) {
            String country = it.next();
            System.out.println("----country"+country);
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(country);
            jsonArray.put("this);

            jsonObj.put(country, jsonArray);
        }

Is there any way I can store the data into my json object inside the while loop itself?

4
  • 3
    A standard JSON object is a set of unordered key/value pairs. It cannot be "sorted". Commented Oct 21, 2013 at 11:36
  • BTW, your illustration does not match your code. Your illustration contains only objects, no arrays. Commented Oct 21, 2013 at 11:37
  • 1.move it to an Object as array 2.sort your array Commented Oct 21, 2013 at 11:39
  • Sorry for the error. I have updated the format for my json. Commented Oct 21, 2013 at 11:40

3 Answers 3

10

Even if this post is quite old, I thought it is worth posting an alternative without GSON:

First store your keys in an ArrayList, then sort it and loop through the ArrayList of keys:

Iterator<String> it= MyTreeSet.iterator();
ArrayList<String>keys = new ArrayList();

while (it.hasNext()) {
    keys.add(it.next());
}
Collections.sort(keys);
for (int i = 0; i < keys.size(); i++) {
    String country = keys.get(i);
    System.out.println("----country"+country);
    JSONArray jsonArray = new JSONArray();
    jsonArray.put(country);
    jsonArray.put("this");

    jsonObj.put(country, jsonArray);
}
Sign up to request clarification or add additional context in comments.

Comments

2

It works with Google Gson API. Try it out.

    try{

        TreeSet<String> MyTreeSet = new TreeSet<String>();
        MyTreeSet.add("SPAIN");
        MyTreeSet.add("TAIWNA");
        MyTreeSet.add("INDIA");
        MyTreeSet.add("JAPAN");

        System.out.println(MyTreeSet);
        Iterator<String> it= MyTreeSet.iterator();
        JsonObject gsonObj = new JsonObject();
        JSONObject jsonObj = new JSONObject();
        while (it.hasNext()) {
            String country = it.next();
            System.out.println("----country"+country);
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(country);
            jsonArray.put("this");

            jsonObj.put(country, jsonArray);

            JsonArray gsonArray = new JsonArray();

            gsonArray.add(new JsonPrimitive("country"));
            gsonArray.add(new JsonPrimitive("this"));
            gsonObj.add(country, gsonArray);
        }
        System.out.println(gsonObj.toString());
        System.out.println(jsonObj.toString());




    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

1 Comment

Thanks alot buddy. It worked really well and got what I expected... :)
0

Below are what the doc says from http://www.json.org/java/index.html.

"A JSONObject is an unordered collection of name/value pairs."

"A JSONArray is an ordered sequence of values. "

In order to get an sorted Json Object, you can use Gson which is already provided a good answer by @user748316.

1 Comment

This answer does not give any useful information regarding to question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.