掌握两种Json工具类的使用
1.Json对象的创建和设定
2.Json对象转字符串
3.Json字符串转实体类
4.字符串转Json对象
5.Json字符串转Json数组
6.Json数组转实体类集合
1.1.导入依赖
cn.hutool
hutool-all
4.1.14
1.2.测试代码
public class testHutool {
public static void main(String[] args) {
JSONObject obj = JSONUtil.createObj();
obj.put("id", 1);
obj.put("name", "林");
obj.put("address", "山东");
obj.put("sex","男");
System.out.println(obj);
String s = JSONUtil.toJsonStr(obj);
System.out.println(s);
Student student = JSONUtil.toBean(s, Student.class);
System.out.println(student);
JSONObject jsonObject = JSONUtil.parseObj(s);
System.out.println(jsonObject);
List stuList = new ArrayList<>();
for (int i = 1; i < 5; i++) {
Student stu = new Student();
stu.setId(i);
stu.setAddress("山东" + i);
stu.setName("林" + i);
stuList.add(stu);
}
String s1 = JSONUtil.toJsonStr(stuList);
System.out.println(s1);
JSONArray objects = JSONUtil.parseArray(stuList);
List students = JSONUtil.toList(objects, Student.class);
System.out.println(students);
}
}
1.3.测试结果
{"address":"山东","sex":"男","name":"林","id":1}
{"address":"山东","sex":"男","name":"林","id":1}
Student{id=1, name='林', address='山东'}
{"address":"山东","sex":"男","name":"林","id":1}
[{"address":"山东1","name":"林1","id":1},{"address":"山东2","name":"林2","id":2},{"address":"山东3","name":"林3","id":3},{"address":"山东4","name":"林4","id":4}]
[Student{id=1, name='林1', address='山东1'}, Student{id=2, name='林2', address='山东2'}, Student{id=3, name='林3', address='山东3'}, Student{id=4, name='林4', address='山东4'}]
2.1导入依赖
com.alibaba
fastjson
1.2.28
2.2测试代码
public class testFastjson {
public static void main(String[] args) {
JSONObject jsonData = new JSONObject();
jsonData.put("id", 1);
jsonData.put("name", "林");
jsonData.put("address", "山东");
System.out.println("-------" + jsonData);
String jsonString = JSON.toJSONString(jsonData);
System.out.println("*******" + jsonString);
JSONObject jsonObject = JSON.parseObject(jsonString);
System.out.println("///" + jsonObject.getString("name"));
Student student = JSON.parseObject(jsonString, Student.class);
System.out.println("|||||||" + student.toString());
List stuList = new ArrayList<>();
for (int i = 1; i < 5; i++) {
Student stu = new Student();
stu.setId(i);
stu.setAddress("山东" + i);
stu.setName("林" + i);
stuList.add(stu);
}
String s = JSON.toJSONString(stuList);
System.out.println(s);
JSONArray objects = JSON.parseArray(JSON.toJSONString(stuList));
System.out.println(objects.stream().iterator().next());
List students = JSON.parseArray(JSON.toJSONString(stuList), Student.class);
System.out.println(students);
}
2.3测试结果
-------{"address":"山东","name":"林","id":1}
*******{"address":"山东","name":"林","id":1}
///林
|||||||Student{id=1, name='林', address='山东'}
[{"address":"山东1","id":1,"name":"林1"},{"address":"山东2","id":2,"name":"林2"},{"address":"山东3","id":3,"name":"林3"},{"address":"山东4","id":4,"name":"林4"}]
{"address":"山东1","name":"林1","id":1}
[Student{id=1, name='林1', address='山东1'}, Student{id=2, name='林2', address='山东2'}, Student{id=3, name='林3', address='山东3'}, Student{id=4, name='林4', address='山东4'}]