如何在Java中自动增加JSONObject的属性?
甲的JSONObject 是的无序集合的名称/值 对和从字符串解析文本以产生映射样的对象。但是,我们可以使用JSONObject类的crement () 方法 自动增加JSONObject的属性。如果没有这样的属性,请创建一个值为1的属性。如果存在这样的属性,并且它是整数,长整型,双精度或浮点型,则将其添加一个。
语法
public JSONObject increment(java.lang.String key) throws JSONException
示例
import org.json.JSONException;
import org.json.JSONObject;
public class IncrementJSONObjectTest {
public static void main(String[] args) throws JSONException { JSONObject jsonObj = new JSONObject();
jsonObj.put("year", 2019);
jsonObj.put("age", 25);
System.out.println(jsonObj.toString(3));
jsonObj.increment("year").increment("age");
System.out.println(jsonObj.toString(3));
jsonObj.increment("year").increment("age");
System.out.println(jsonObj.toString(3));
jsonObj.increment("year").increment("age");
System.out.println(jsonObj.toString(3));
}
}输出结果
{
"year": 2019,
"age": 25
}
{
"year": 2020,
"age": 26
}
{
"year": 2021,
"age": 27
}
{
"year": 2022,
"age": 28
}热门推荐
8 怎么说祝福语简短
10 分手祝福语简短短句
11 节日祝福语幽默简短的
12 年月日祝福语简短
13 婚庆花篮祝福语简短大气
14 国庆求婚祝福语简短精辟
15 牛年女神节祝福语简短
16 祝女性生日祝福语简短
17 男朋友祝福语简短新年
18 妈妈给宝贝祝福语简短