本文转自测试人社区,原文链接: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);
}