gpt4 book ai didi

java - 无法解析其中包含 List 元素的 json

转载 作者:行者123 更新时间:2023-12-02 12:13:47 24 4
gpt4 key购买 nike

我有一个Java类,里面有2个List对象,我是Json序列化父类。

@JsonSerialize
public class RequestSalesJson {
@JsonProperty("nonUniqueSalesList")
private List<SalesDataJson> getNonUniqueSalesDataJson;
@JsonProperty("uniqueSalesList")
private List<SalesDataJson> uniqueSalesDataJson;

public List<SalesDataJson> getGetNonUniqueSalesDataJson() {
return getNonUniqueSalesDataJson;
}

public void setGetNonUniqueSalesDataJson(List<SalesDataJson> getNonUniqueSalesDataJson) {
this.getNonUniqueSalesDataJson = getNonUniqueSalesDataJson;
}

public List<SalesDataJson> getUniqueSalesDataJson() {
return uniqueSalesDataJson;
}

public void setUniqueSalesDataJson(List<SalesDataJson> uniqueSalesDataJson) {
this.uniqueSalesDataJson = uniqueSalesDataJson;
}
}

SalesReturnJson.java

@JsonSerialize
public class SalesReturnJson {
@JsonProperty("starttime")
private String startTime;
@JsonProperty("pn")
private String partNumber;
@JsonProperty("so")
private String SalesOrderNumber;
@JsonProperty("wo")
private String workOrderNumber;
@JsonProperty("loc")
//other variables declared..
}

Controller.java :-

@RequestMapping(value = "/addAllSalesData",method = RequestMethod.POST)
public void addAllSalesData(@RequestBody RequestSalesJson requestSalesJsons){
log.info("POST : '/addSalesData'");
try{
System.out.print("In Controller "+requestSalesJsons.getUniqueSalesDataJson());
//salesService.processSalesData(requestSalesJsons);
}
catch(Exception e){
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}

这里的值即将为空。

下面是我正在使用的 json :-

{ "uniqueSalesJson": [{"SO":4000955,"Part Number":"000","Locator":255638,"Lot Number":"P01-2059139","Reservation Quantity":2,"Status":"Released to warehouse","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"},{"SO":4000955,"Part Number":"000","Locator":255652,"Lot Number":"P01-2059140","Reservation Quantity":10,"Status":"Released to warehouse","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"}],"nonUniqueSalesJson":[{"SO":4000992,"Part Number":"1276M84G15","Locator":12345,"Lot Number":"P01-2344141","Reservation Quantity":6,"Status":"PACKED","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"},{"SO":4000992,"Part Number":"1276M84G15","Locator":12345,"Lot Number":"P01-2344141","Reservation Quantity":6,"Status":"PICKED","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"}]}

最佳答案

您的代码中存在一些问题,让我怀疑您的应用程序能否编译。首先,将 SalesReturnJson 类重命名为 SalesDataJson。然后检查您的 @JsonProperty 注释。这里的值必须与 Json 字符串中的属性键完全匹配。重构所有这些内容将引导您到达根实体类:

@JsonSerialize
public class RequestSalesJson {
@JsonProperty("nonUniqueSalesJson")
private List<SalesDataJson> nonUniqueSalesDataJson;
@JsonProperty("uniqueSalesJson")
private List<SalesDataJson> uniqueSalesDataJson;
...
}

和您的 SalesDataJson 类(缺少映射器通过配置忽略的许多属性):

@JsonSerialize
public class SalesDataJson {
@JsonProperty("Start_Time")
private String startTime;
@JsonProperty("Part Number")
private String partNumber;
@JsonProperty("SO")
private String SalesOrderNumber;
}

此示例与 com.fasterxml.jackson.databind.ObjectMapper 一起按预期工作

希望有帮助!

关于java - 无法解析其中包含 List 元素的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46344991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com