需要帮忙解析这个 JSON。

huangapple go评论72阅读模式
英文:

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.

{
    &quot;report_title&quot;: &quot;title&quot;,
    &quot;result_date&quot;: &quot;08/12/2020 22:40:51&quot;,
    &quot;company&quot;: &quot;company name&quot;,
    &quot;add1&quot;: &quot;123 main st&quot;,
    &quot;add2&quot;: null,
    &quot;city&quot;: &quot;some city&quot;,
    &quot;state&quot;: &quot;state&quot;,
    &quot;country&quot;: &quot;United States of America&quot;,
    &quot;zip&quot;: &quot;12345&quot;,
    &quot;name&quot;: &quot;first last&quot;,
    &quot;username&quot;: &quot;firstlast&quot;,
    &quot;role&quot;: &quot;Manager&quot;
},
{
    &quot;launch_date&quot;: &quot;08/12/2020 19:05:19&quot;,
    &quot;client&quot;: &quot;client1&quot;,
    &quot;order_number&quot;: &quot;1&quot;,
    &quot;total_orders&quot;: &quot;1&quot;,
    &quot;type&quot;: &quot;type&quot;,
    &quot;status&quot;: &quot;Finished&quot;,
    &quot;reference&quot;: &quot;order reference&quot;,
    &quot;fulfill&quot;: &quot;some name&quot;,
    &quot;duration&quot;: &quot;00:07:12&quot;,
    &quot;order title&quot;: &quot;title&quot;,
    &quot;order_groups&quot;: null,
    &quot;part_nums&quot;: &quot;1,2,3&quot;,
  },
  {
  &quot;part_num&quot;: &quot;1234&quot;,
  &quot;name&quot;: &quot;asdf&quot;,
  &quot;other_name&quot;: &quot;asdf&quot;,
  &quot;manufacturer&quot;: &quot;aasdf&quot;,
  &quot;status&quot;: &quot;asdfasf&quot;,
  &quot;id&quot;: 12345,
  &quot;title&quot;: &quot;some title&quot;,
  &quot;type&quot;: &quot;Ig&quot;,
  &quot;notes&quot;: &quot;long winded notes&quot;,
  &quot;impact&quot;: null,
  &quot;solution&quot;: null,
  },
  {
  &quot;part_num&quot;: &quot;1235&quot;,
  &quot;name&quot;: &quot;asdf1&quot;,
  &quot;other_name&quot;: &quot;asdf1&quot;,
  &quot;manufacturer&quot;: &quot;aasdf1&quot;,
  &quot;status&quot;: &quot;asdfasf1&quot;,
  &quot;id&quot;: 12346,
  &quot;title&quot;: &quot;some title&quot;,
  &quot;type&quot;: &quot;Ig&quot;,
  &quot;notes&quot;: &quot;long winded notes&quot;,
  &quot;impact&quot;: null,
  &quot;solution&quot;: null,
  }
...

I created four classes: Customer, OrderInfo, OrderDetail and encompassing class Order.

class Order {
    Customer customer
    OrderInfo orderInfo;
    List&lt;OrderDetail&gt; 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(&quot;report_title&quot;) private String reportTitle;
    @JsonProperty(&quot;result_date&quot;) private String resultDate;
    @JsonProperty(&quot;company&quot;) 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);

huangapple
  • 本文由 发表于 2020年8月27日 01:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63602912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定