public class User {
private Integer id;
private String name;
private String address;
private Date birth;
public User() {
}
public User(Integer id, String name, String address, Date birth) {
this.id = id;
this.name = name;
this.address = address;
this.birth = birth;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
public class Test01FastJson {
public static void main(String[] args) {
User user=new User(1,"小明","武汉",new Date());
//对象 -> JOSN字符串
System.out.println(JSON.toJSONString(user));
//对象 -> JSONObject
//转化成JSON对象 本质也就是一个Map
JSONObject jsonObject = (JSONObject)JSON.toJSON(user);
System.out.println(jsonObject.get("id"));
System.out.println(jsonObject.get("name"));
System.out.println(jsonObject.get("address"));
System.out.println(jsonObject.get("birth"));
}
}
public class Test02FastJson {
public static void main(String[] args) {
List<User> users=new ArrayList<>();
for (int i = 1; i <=5 ; i++) {
users.add(new User(i,"小明"+i,"武汉"+i,new Date()));
}
//list对象 -> JSON字符串
String s = JSON.toJSONString(users);
System.out.println(s);
//list对象 -> JSONArray
//转化成JSON对象 本质也就是一个Map
JSONArray array = (JSONArray)JSON.toJSON(users);
for (Object o : array) {
if (o instanceof JSONObject){
JSONObject o1 = (JSONObject) o;
System.out.println(o1.get("id")+" "+o1.get("name")+" "+o1.get("address")+" "+o1.get("birth"));
}
}
}
}
public class Test03FastJson {
public static void main(String[] args) {
String json="{\"address\":\"武汉\",\"birth\":1609943410183,\"id\":1,\"name\":\"小明\"}";
//json字符串 -> JSONObject
JSONObject jsonObject = (JSONObject)JSON.parse(json);
System.out.println(jsonObject);
System.out.println(jsonObject.get("id"));
System.out.println(jsonObject.get("address"));
System.out.println(jsonObject.get("name"));
System.out.println(jsonObject.get("birth"));
//json字符串 -> 对象
User user = JSON.parseObject(json, User.class);
System.out.println(user.getId()+" "+user.getName()+" "+user.getAddress()+" "+user.getBirth());
}
}
public class Test04FastJson {
public static void main(String[] args) {
String json="[{\"address\":\"武汉1\",\"birth\":1609943580570,\"id\":1,\"name\":\"小明1\"},{\"address\":\"武汉2\",\"birth\":1609943580570,\"id\":2,\"name\":\"小明2\"},{\"address\":\"武汉3\",\"birth\":1609943580570,\"id\":3,\"name\":\"小明3\"},{\"address\":\"武汉4\",\"birth\":1609943580570,\"id\":4,\"name\":\"小明4\"},{\"address\":\"武汉5\",\"birth\":1609943580570,\"id\":5,\"name\":\"小明5\"}]";
//JSON字符串 -> JSONArray
JSONArray array = (JSONArray)JSON.parse(json);
for (Object o : array) {
if (o instanceof JSONObject){
JSONObject o1 = (JSONObject) o;
System.out.println(o1.get("id")+" "+o1.get("name")+" "+o1.get("address")+" "+o1.get("birth"));
}
}
System.out.println();
//JSON字符串 -> list
List<User> users = JSON.parseArray(json, User.class);
for (User user : users) {
System.out.println(user.getId()+" "+user.getName()+" "+user.getAddress()+" "+user.getBirth());
}
}
}