How to fluently build JSON in Java?

后端 未结 9 1002
别跟我提以往
别跟我提以往 2020-12-07 13:43

I\'m thinking of something like:

String json = new JsonBuilder()
  .add(\"key1\", \"value1\")
  .add(\"key2\", \"value2\")
  .add(\"key3\", new JsonBuilder()         


        
9条回答
  •  离开以前
    2020-12-07 14:07

    I am using the org.json library and found it to be nice and friendly.

    Example:

    String jsonString = new JSONObject()
                      .put("JSON1", "Hello World!")
                      .put("JSON2", "Hello my World!")
                      .put("JSON3", new JSONObject().put("key1", "value1"))
                      .toString();
    
    System.out.println(jsonString);
    

    OUTPUT:

    {"JSON2":"Hello my World!","JSON3":{"key1":"value1"},"JSON1":"Hello World!"}
    

提交回复
热议问题