如何在Java中自动增加JSONObject的属性?

WBOY
WBOY 转载
2023-08-19 14:41:04 146浏览

如何在Java中自动增加JSONObject的属性?

语法

public JSONObject increment(java.lang.String key) throws JSONException

Example

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
}

以上就是如何在Java中自动增加JSONObject的属性?的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除