文章目录
-
- 1、使用JSONObject()生成字符串
- 2、拼接Json字符串
1、使用JSONObject()生成字符串
/**
* 使用JSONObject 组合字符串
*/
@Test
public void Demo1(){
JSONObject jsonObj = new JSONObject();
jsonObj.put("name","张三");
jsonObj.put("age",18);
String student = JSONObject.toJSONString(jsonObj);
System.out.println(student);
}
输出如下:
2、拼接Json字符串
/**
* 拼接JSON字符串
*/
@Test
public void Demo2(){
String json_to_string = "{\"name\":\"李四\"," + "\"age" +
"\":\"18\"}";
System.out.println(json_to_string);
}
输出如下: