JSON数据写入和解析

时间:2023-05-27 22:16:14

如何写入JSON

需要第三方jar包,JSON包

//写入json数据

public static String sendJson() {
JSONObject json = new JSONObject();
User user = new User("username", "address", "password", 18);
json.put("user", user);
return json.get("user").toString();
}

//得到json数据

public static void main(String[] args) {
JSONObject jsonObject = JSONObject.fromObject(sendJson());
String username = jsonObject.getString("username");
System.out.println(username);
}