需要帮助解析这个 JSON
Need help parsing this JSON
我有以下格式的 JSON 文件。第一个对象是客户信息,第二个是订单信息,然后是 n 个对象,表示每个订购零件的详细信息。我需要将其转换为 Java POJO。
这就是 JSON 的样子。它来自外部供应商,所以我必须按原样使用它。
{
"report_title": "title",
"result_date": "08/12/2020 22:40:51",
"company": "company name",
"add1": "123 main st",
"add2": null,
"city": "some city",
"state": "state",
"country": "United States of America",
"zip": "12345",
"name": "first last",
"username": "firstlast",
"role": "Manager"
},
{
"launch_date": "08/12/2020 19:05:19",
"client": "client1",
"order_number": "1",
"total_orders": "1",
"type": "type",
"status": "Finished",
"reference": "order reference",
"fulfill": "some name",
"duration": "00:07:12",
"order title": "title",
"order_groups": null,
"part_nums": "1,2,3",
},
{
"part_num": "1234",
"name": "asdf",
"other_name": "asdf",
"manufacturer": "aasdf",
"status": "asdfasf",
"id": 12345,
"title": "some title",
"type": "Ig",
"notes": "long winded notes",
"impact": null,
"solution": null,
},
{
"part_num": "1235",
"name": "asdf1",
"other_name": "asdf1",
"manufacturer": "aasdf1",
"status": "asdfasf1",
"id": 12346,
"title": "some title",
"type": "Ig",
"notes": "long winded notes",
"impact": null,
"solution": null,
}
...
我创建了四个 classes:Customer
、OrderInfo
、OrderDetail
并包含 class Order
.
class Order {
Customer customer
OrderInfo orderInfo;
List<OrderDetail> orderDetailList;
}
我正在尝试使用 ObjectMapper
,但我只获得了客户对象,没有别的。任何帮助表示赞赏。
编辑:
这是我试过的
public static void main (String [] args) {
File inputFile = new File(filepath);
String content = Files.readString(Path.of(inputFile.getPath()), StandardCharsets.US_ASCII);
ObjectMapper mapper = new ObjectMapper();
Customer customer = mapper.readValue(content, Customer.class);
OrderInfo orderInfo = mapper.readValue(content, OrderInfo.class);
}
@Data
@NoArgsConstructor
@JsonIgnoreProperties( ignoreUnknown = true )
public class Customer {
@JsonProperty("report_title") private String reportTitle;
@JsonProperty("result_date") private String resultDate;
@JsonProperty("company") private String company;
...
}
我想我发布问题有点太早了。
我接受了 Ajay Kr Choudhary 的建议,只是分别解析每个节点并分别映射它们。没有我想要的那么高效或自动化,但总比没有好。
JsonNode rootNode = mapper.readTree(inputFile);
Customer customer = mapper.treeToValue(rootNode.get(0), Customer.class);
OrderInfo orderInfo = mapper.treeToValue(rootNode.get(1), OrderInfo.class);
OrderDetail orderDetail = mapper.treeToValue(rootNode.get(2), OrderDetail.class);
我有以下格式的 JSON 文件。第一个对象是客户信息,第二个是订单信息,然后是 n 个对象,表示每个订购零件的详细信息。我需要将其转换为 Java POJO。
这就是 JSON 的样子。它来自外部供应商,所以我必须按原样使用它。
{
"report_title": "title",
"result_date": "08/12/2020 22:40:51",
"company": "company name",
"add1": "123 main st",
"add2": null,
"city": "some city",
"state": "state",
"country": "United States of America",
"zip": "12345",
"name": "first last",
"username": "firstlast",
"role": "Manager"
},
{
"launch_date": "08/12/2020 19:05:19",
"client": "client1",
"order_number": "1",
"total_orders": "1",
"type": "type",
"status": "Finished",
"reference": "order reference",
"fulfill": "some name",
"duration": "00:07:12",
"order title": "title",
"order_groups": null,
"part_nums": "1,2,3",
},
{
"part_num": "1234",
"name": "asdf",
"other_name": "asdf",
"manufacturer": "aasdf",
"status": "asdfasf",
"id": 12345,
"title": "some title",
"type": "Ig",
"notes": "long winded notes",
"impact": null,
"solution": null,
},
{
"part_num": "1235",
"name": "asdf1",
"other_name": "asdf1",
"manufacturer": "aasdf1",
"status": "asdfasf1",
"id": 12346,
"title": "some title",
"type": "Ig",
"notes": "long winded notes",
"impact": null,
"solution": null,
}
...
我创建了四个 classes:Customer
、OrderInfo
、OrderDetail
并包含 class Order
.
class Order {
Customer customer
OrderInfo orderInfo;
List<OrderDetail> orderDetailList;
}
我正在尝试使用 ObjectMapper
,但我只获得了客户对象,没有别的。任何帮助表示赞赏。
编辑: 这是我试过的
public static void main (String [] args) {
File inputFile = new File(filepath);
String content = Files.readString(Path.of(inputFile.getPath()), StandardCharsets.US_ASCII);
ObjectMapper mapper = new ObjectMapper();
Customer customer = mapper.readValue(content, Customer.class);
OrderInfo orderInfo = mapper.readValue(content, OrderInfo.class);
}
@Data
@NoArgsConstructor
@JsonIgnoreProperties( ignoreUnknown = true )
public class Customer {
@JsonProperty("report_title") private String reportTitle;
@JsonProperty("result_date") private String resultDate;
@JsonProperty("company") private String company;
...
}
我想我发布问题有点太早了。
我接受了 Ajay Kr Choudhary 的建议,只是分别解析每个节点并分别映射它们。没有我想要的那么高效或自动化,但总比没有好。
JsonNode rootNode = mapper.readTree(inputFile);
Customer customer = mapper.treeToValue(rootNode.get(0), Customer.class);
OrderInfo orderInfo = mapper.treeToValue(rootNode.get(1), OrderInfo.class);
OrderDetail orderDetail = mapper.treeToValue(rootNode.get(2), OrderDetail.class);