How to maintain the order of a JSONObject

后端 未结 12 1608
栀梦
栀梦 2020-12-11 05:49

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.         


        
相关标签:
12条回答
  • 2020-12-11 06:35

    try this

    JSONObject jsonObject = new JSONObject(jsonString) {
        /**
         * changes the value of JSONObject.map to a LinkedHashMap in order to maintain
         * order of keys.
         */
        @Override
        public JSONObject put(String key, Object value) throws JSONException {
            try {
                Field map = JSONObject.class.getDeclaredField("map");
                map.setAccessible(true);
                Object mapValue = map.get(this);
                if (!(mapValue instanceof LinkedHashMap)) {
                    map.set(this, new LinkedHashMap<>());
                }
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            return super.put(key, value);
        }
    };
    jsonObject.remove("owner");
    jsonString=jsonObject.toString();
    
    0 讨论(0)
  • 2020-12-11 06:36

    I was able to do this with help of classpath overriding.

    1. created package package org.json.simple which is same as in jar and class named as JSONObject.
      1. Took existing code from jar and updated the class by extending LinkedHashmap instead of Hashmap

    by doing these 2 steps it will maintain the order, because preference of picking `JSONObject will be higher to pick from the new package created in step 1 than the jar.

    0 讨论(0)
  • 2020-12-11 06:37

    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.

    0 讨论(0)
  • 2020-12-11 06:39

    From Android 20, JSONObject preserves the order as it uses LinkedHashMap to store namevaluepairs. Android 19 and below uses HashMap to store namevaluepairs. So, Android 19 and below doesn't preserve the order. If you are using 20 or above, don't worry, JSONObject will preserve the order. Or else, use JSONArray instead.

    0 讨论(0)
  • 2020-12-11 06:40

    You can go for the JsonObject provided by the com.google.gson it is nearly the same with the JSONObject by org.json but some different functions. For converting String to Json object and also maintains the order you can use:

    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(<Json String>, JsonObject.class);
    

    For eg:-

    String jsonString = "your json String";
    
    JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
    

    It just maintains the order of the JsonObject from the String.

    0 讨论(0)
  • 2020-12-11 06:41

    In JDK 8 and above, We can do it by using nashorn engine, supported in JDK 8. Java 8 support to use js engine to evaluate:

    String content = ..json content...  
    String name = "test";  
    String result = (String) engine.eval("var json = JSON.stringify("+content+");"
                                    + "var jsResult = JSON.parse(json);"
                                    + "jsResult.name = \"" + name + "\";"
                                    + "jsResult.version = \"1.0\";"
                                    + "JSON.stringify( jsResult );"
                                );
    
    0 讨论(0)
提交回复
热议问题