简体   繁体   English

将值添加到 Java 中的 JSONObject

[英]Prepend value to a JSONObject in Java

Is there a possible way to prepend a value to a JSONObject in Java?有没有一种可能的方法来为 Java 中的 JSONObject 添加一个值?

For instance, I have a JSONObject that says:例如,我有一个 JSONObject 说:

{
    "one" : 1,
    "two" : 2,
    "three" : 3,
    "four" : 4,
    "five" : 5
}

Now I want to add another value to it, but if I say:现在我想为其添加另一个值,但如果我说:

jsonObject.put("zero", 0);

I will be having something like this:我会有这样的事情:

{
    "one" : 1,
    "two" : 2,
    "three" : 3,
    "four" : 4,
    "five" : 5,
    "zero" : 0
}

But is there a way I can prepend a new value maybe something like jsonObject.prepend("zero", 0);但是有没有办法我可以预先添加一个新值,比如jsonObject.prepend("zero", 0); so I will be having所以我会有

{
    "zero" : 0,
    "one" : 1,
    "two" : 2,
    "three" : 3,
    "four" : 4,
    "five" : 5
}

Note: I am having a random keys, just used this one as example.注意:我有一个随机密钥,只是以这个为例。 I don't even know the keys, I extract them from my database, so there is no way I should be sorting the keys我什至不知道密钥,我从数据库中提取它们,所以我不可能对密钥进行排序

Okay... I had to find a way out to this one.好吧...我必须想办法解决这个问题。 It look's little bit dirty, but it works perfectly well.它看起来有点脏,但效果很好。

I had to create my own JsonObject class extending JSONObject to it, then add a prepend method我必须创建自己的JsonObject class 扩展JSONObject到它,然后添加一个前置方法

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class JsonObject extends JSONObject {

    //create method to prepend object
    public JsonObject prepend(String key, Object object) throws JSONException {
        JSONArray keysArray = this.names();  //save the object keys
        JSONArray objArray = new JSONArray();
        Iterator iterator = this.keys();
        while (iterator.hasNext()){
            String k = (String) iterator.next();
            objArray.put(this.get(k));   //save the object
        }
        this.removeAll();    // empty the JsonObject
        this.put(key, object);    // now it's empty, put the new object, making it to be the 1st(prepending trick)
        assert keysArray != null;
        if(keysArray.length() > 0) {
            for(int i = 0; i < keysArray.length(); i++) {
                String exKey = keysArray.getString(i);
                assert objArray != null;
                Object o = objArray.get(i);
                if(!key.equals(exKey))
                    this.put(exKey, o);    // put back the saved keys and object
            }
        }
        return this;
    }

    // method to empty the JsonObject
    public void removeAll() throws JSONException {
        JSONArray array = this.names();
        assert array != null;
        if(array.length() > 0) {
            for(int i = 0; i < array.length(); i++) {
                String key = array.getString(i);
                this.remove(key);
            }
        }
    }

}

Then using it in my code然后在我的代码中使用它

JsonObject object = new JsonObject();
object.put("one", 1);
object.put("two", 2);
object.put("three", 3);
object.put("four", 4);
object.put("five", 5); 
object.prepend("zero", 0);
Log.i("object" object.toString());

Result结果

{
    "zero" : 0,
    "one" : 1,
    "two" : 2,
    "three" : 3,
    "four" : 4,
    "five" : 5
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM