本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/28204
pom文件导入
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
解析list-json
orderlist.json
[
{
"item": "No. 9 Sprockets",
"quantity": 12,
"unitPrice": 1.23,
"orderDate": "2019-04-17"
},
{
"item": "No. Widget (10mm)",
"quantity": 10,
"unitPrice": 3.45,
"orderDate": "2022-01-16"
}
]
List类型解析
- 直接声明类型{style=width:500px}
@Test
void orderListMap() throws IOException {
ObjectMapper mapper = new ObjectMapper(new JsonFactory());
TypeReference<List<HashMap<String, Object>>> typeReference =
new TypeReference<List<HashMap<String, Object>>>() {};
mapper.findAndRegisterModules();
List<HashMap<String, Object>> listHashMap =
mapper.readValue(
new File("src/test/resources/json/orderlist.json"),
typeReference);
System.out.println(listHashMap.toString());
}
实体类声明
声明实体类OrderLine
@Data
public class OrderLine{
private String item;
private int quantity;
private BigDecimal unitPrice;
private LocalDate orderDate;
public OrderLine() {
}
public OrderLine(String item, int quantity,
BigDecimal unitPrice, LocalDate orderDate) {
this.item = item;
this.quantity = quantity;
this.unitPrice = unitPrice;
this.orderDate = orderDate;
}
}
实体类对应解析
@Test
void orderLinesTest() throws IOException {
ObjectMapper mapper = new ObjectMapper(new JsonFactory());
TypeReference<List<OrderLine>> typeReference =
new TypeReference<List<OrderLine>>() {};
mapper.findAndRegisterModules();
List<OrderLine> orderLines =
mapper.readValue(
new File("src/test/resources/json/orderlist.json"),
typeReference);
System.out.println(orderLines);
}
解析无规律json
order.json
{
"orderNo": "A001",
"date": "2019-04-17",
"customerName": "Customer, Joe",
"orderLines": [
{
"item": "No. 9 Sprockets",
"quantity": 12,
"unitPrice": 1.23
},
{
"item": "No. Widget (10mm)",
"quantity": 40,
"unitPrice": 3.45
}
]
}
类型解析
- 直接声明类型{style=width:500px}
@Test
void orderMap() throws IOException {
ObjectMapper mapper = new ObjectMapper(new JsonFactory());
TypeReference<HashMap<String, Object>> typeReference =
new TypeReference<HashMap<String, Object>>() {};
mapper.findAndRegisterModules();
HashMap<String, Object> stringObjectHashMap =
mapper.readValue(
new File("src/test/resources/json/order.json"),
typeReference);
System.out.println(stringObjectHashMap.toString());
}
实体类声明
声明实体类Order
@Data
public class Order {
private String orderNo;
private LocalDate date;
private String customerName;
private List<OrderLine> orderLines;
public Order() {
}
public Order(String orderNo, LocalDate date,
String customerName, List<OrderLine> orderLines) {
this.orderNo = orderNo;
this.date = date;
this.customerName = customerName;
this.orderLines = orderLines;
}
}
实体类对应解析
@Test
void orderTest() throws IOException {
ObjectMapper mapper = new ObjectMapper(new JsonFactory());
TypeReference<Order> typeReference =
new TypeReference<Order>() {};
mapper.findAndRegisterModules();
Order order =
mapper.readValue(
new File("src/test/resources/json/order.json"),
typeReference);
System.out.println(order);
}
推荐学习
【霍格沃兹测试开发】7天软件测试快速入门带你从零基础/转行/小白/就业/测试用例设计实战
【霍格沃兹测试开发】最新版!Web 自动化测试从入门到精通/ 电子商务产品实战/Selenium (上集)
【霍格沃兹测试开发】最新版!Web 自动化测试从入门到精通/ 电子商务产品实战/Selenium (下集)
【霍格沃兹测试开发】明星讲师精心打造最新Python 教程软件测试开发从业者必学(上集)
【霍格沃兹测试开发】明星讲师精心打造最新Python 教程软件测试开发从业者必学(下集)
【霍格沃兹测试开发】精品课合集/ 自动化测试/ 性能测试/ 精准测试/ 测试左移/ 测试右移/ 人工智能测试
【霍格沃兹测试开发】腾讯/ 百度/ 阿里/ 字节测试专家技术沙龙分享合集/ 精准化测试/ 流量回放/Diff
【霍格沃兹测试开发】Pytest 用例结构/ 编写规范 / 免费分享
【霍格沃兹测试开发】JMeter 实时性能监控平台/ 数据分析展示系统Grafana/Docker 安装
【霍格沃兹测试开发】接口自动化测试的场景有哪些?为什么要做接口自动化测试?如何一键生成测试报告?
【霍格沃兹测试开发】面试技巧指导/ 测试开发能力评级/1V1 模拟面试实战/ 冲刺年薪百万!
【霍格沃兹测试开发】腾讯软件测试能力评级标准/ 要评级表格的联系我
【霍格沃兹测试开发】Pytest 与Allure2 一键生成测试报告/ 测试用例断言/ 数据驱动/ 参数化
【霍格沃兹测试开发】App 功能测试实战快速入门/adb 常用命令/adb 压力测试
【霍格沃兹测试开发】阿里/ 百度/ 腾讯/ 滴滴/ 字节/ 一线大厂面试真题讲解,卷完拿高薪Offer !
【霍格沃兹测试开发】App自动化测试零基础快速入门/Appium/自动化用例录制/参数配置
【霍格沃兹测试开发】如何用Postman 做接口测试,从入门到实战/ 接口抓包(最新最全教程)
【霍格沃兹测试开发】6 小时轻松上手功能测试/ 软件测试工作流程/ 测试用例设计/Bug 管理
【霍格沃兹测试开发】零基础小白如何使用Postman ,从零到一做接口自动化测试/ 从零基础到进阶到实战
【霍格沃兹测试开发】建议收藏全国CCF 测试开发大赛Python 接口自动化测试赛前辅导 / 项目实战
更多软件测试开发视频教程点此
软件测试职业发展
零基础入门
测试必备编程篇
自动化测试
性能测试
测试管理
工程效能篇
面试求职篇
软件测试的面试宝典,内含一线互联网大厂面试真题、面试技巧、软件测试面试简历指导,免费领取!