Java 如何根据 Key 对 JSON 对象进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19493413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:44:17  来源:igfitidea点击:

How can i sort my JSON object based on Key?

javajson

提问by AppleBud

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:

我正在创建一个 JSON 对象,在其中添加一个键和一个数组值。key 和 value 的值都来自具有排序形式的数据的 TreeSet。但是,当我在 json 对象中插入数据时,它是随机存储的,没有任何顺序。这是我目前的 json 对象:

{
    "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?

有什么方法可以将数据存储到 while 循环本身内的 json 对象中吗?

采纳答案by user748316

It works with Google Gson API. Try it out.

它适用于 Google Gson API。试试看。

    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();
    }

回答by deepSleep

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

以下是来自http://www.json.org/java/index.html的文档所说的内容。

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

“JSONObject 是名称/值对的无序集合。”

"A JSONArray is an ordered sequence of values. "

“JSONArray 是一个有序的值序列。”

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

为了获得排序的 Json 对象,您可以使用 Gson,@user748316 已经提供了一个很好的答案。

回答by Christian

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

即使这篇文章已经很老了,我认为值得发布一个没有 GSON 的替代方案:

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

首先将您的键存储在 ArrayList 中,然后对其进行排序并遍历键的 ArrayList:

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);
}