gpt4 book ai didi

java - 在 json java 中切换值

转载 作者:行者123 更新时间:2023-12-01 11:40:47 25 4
gpt4 key购买 nike

如何每次切换 boolean 值,然后将其分配给 json 中的“test”键?

JSONObject json=new JSONObject();
json.put("test",true);
return json.toString();

最佳答案

您必须获取当前值,翻转它,然后将其设置回来。

String jsonText = "{\"test\":true}"; // or wherever you have your json
JSONObject json = new JSONObject(jsonText);
if (json.has("test")) {
json.put("test", !json.getBoolean("test"));
} else {
json.put("test", true);
}

关于java - 在 json java 中切换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29543397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com