简体   繁体   English

切换不起作用-Java

[英]Toggle doesn't work - java

I am trying this to toggle the value of key1 in but I get the same value all the time. 我试图这样做以切换key1的值,但是我一直都得到相同的值。 What am I doing wrong here? 我在这里做错了什么?

boolean jsonText =  true;
JSONObject jsonObj = new JSONObject();

public void setjsonObject(boolean val){
    this.jsonText = val;
}

public Boolean getjsonObject(){
    return this.jsonText;
}

@POST("/hello")
    @PermitAll
    public String hello(String text) {

    try {   
        if (jsonObj.has("key1")) {
            boolean val = !jsonObj.getBoolean("key1");
            jsonObj.remove("key1");
            jsonObj.put("key1", val);
        } else {
            jsonObj.put("key1", true);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return "some result";

    }

Am I resetting the " key1 " boolean value somewhere? 我要在某个地方重置“ key1boolean值吗?

you are creating new JSON object every time: 您每次都在创建新的JSON对象:

JSONObject jsonObj = new JSONObject();

so it never has any value for "key1" and it is always set to true after your hello code. 因此它永远不会具有“ key1”的任何值,并且在hello代码之后始终将其设置为true。

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

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