英文:
Need help parsing this JSON
问题
以下是翻译好的部分:
class Order {
    Customer customer;
    OrderInfo orderInfo;
    List<OrderDetail> orderDetailList;
}
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;
    ...
}
英文:
I have this JSON file in following format. The first object is customer information, the second is order information, followed by n number of objects for details of each part ordered. I need to convert this to Java POJOs.
This is what JSON looks like. It's from external vendor, so I have to work with it as it is.
{
    "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,
  }
...
I created four classes: Customer, OrderInfo, OrderDetail and encompassing class Order.
class Order {
    Customer customer
    OrderInfo orderInfo;
    List<OrderDetail> orderDetailList;
}
I am trying to use ObjectMapper, but I only ever get the customer object and nothing else. Any help appreciated.
EDIT:
Here is what I tried
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;
...
}
答案1
得分: 1
我猜我发布了问题有点早。
我采纳了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);
英文:
I guess I posted the question little too early.
I took Ajay Kr Choudhary's suggestion and just parsed each node separately and mapped them separately. Not as efficient or automated as I wanted, but still it's better than nothing.
 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论