gpt4 book ai didi

java - 在Java中解析JSON返回空结果

转载 作者:行者123 更新时间:2023-12-02 09:01:23 25 4
gpt4 key购买 nike

我正在尝试使用 Java 解析 JSON,但没有得到预期的结果。示例 json:

            "productId": "NDAtOS0wLS0=",
"branchId": 5,
"branchCustomers":[
{"branchId":615,"customerNumber":4918,"products":[]},
{"branchId":615,"customerNumber":9753,"products":[]},
{"branchId":615,"customerNumber":9761,"products":[]}
],
"customerNumber": 240,
"subAccountNumber": 0,
"productType": 9,
"openingDate": "2016-10-02",
"values": [
{
"key": "accountingMethod",
"value": "1"
},
{
"key": "accountType",
"value": "1"
},
{
"key": "assetCashAccountId-branchId",
"value": "615"
},
{
"key": "assetCashAccountId-customerNumber",
"value": "4041240"
},
{
"key": "assetCashAccountId-subAccountNumber",
"value": "2"
},
{
"key": "assetMarginAccountId-branchId",
"value": "615"
},
{
"key": "assetMarginAccountId-customerNumber",
"value": "4041240"
},
{
"key": "assetMarginAccountId-subAccountNumber",
"value": "2"
},
{
"key": "blockingAmount",
"value": "1000000"
},
{
"key": "collateral",
"value": "C"
},
{
"key": "collateralBlockingType",
"value": "1"
},
{
"key": "executingSecurityAccountId-branchId",
"value": "615"
},
{
"key": "executingSecurityAccountId-customerNumber",
"value": "4041240"
},
{
"key": "executingSecurityAccountId-subAccountNumber",
"value": "0"
},
{
"key": "limit",
"value": "1000000"
},
{
"key": "marginAccountId-branchId",
"value": "0"
},
{
"key": "marginAccountId-customerNumber",
"value": "0"
},
{
"key": "marginAccountId-subAccountNumber",
"value": "0"
},
{
"key": "marginMarkup",
"value": "100"
},
{
"key": "rolfNolanLedger",
"value": "B01429"
},
{
"key": "settlementMethod",
"value": "1"
}
]
}
]
}],
"instances": []
}

并非所有 JSON 都具有这种结构,有些可能会丢失某些字段。我创建了一些 DTO 类来解析它。这是我的代码:

public class Response {

private String partnerId;
private byte branchId;
private long customerNumber;
private Long subAccountNumber;
private Byte productType;
private String openingDate;
private String closingDate;
private List<Values> values;
private List<Instances> instances;
private List<BranchCustomers> branchCustomers;


public String getProductid() {
return partnerId;
}

public void setProductid(String productid) {
this.partnerId = productid;
}

public byte getBranchid() {
return branchId;
}

public void setBranchid(byte branchid) {
this.branchId = branchid;
}

public long getCustomernumber() {
return customerNumber;
}

public void setCustomernumber(long customernumber) {
this.customerNumber = customernumber;
}

public Long getSubaccountnumber() {
return subAccountNumber;
}

public void setSubaccountnumber(Long subAccountNumber) {
this.subAccountNumber = subAccountNumber;
}

public Byte getProducttype() {
return productType;
}

public void setProducttype(Byte productType) {
this.productType = productType;
}

public String getOpeningdate() {
return openingDate;
}

public void setOpeningdate(String openingDate) {
this.openingDate = openingDate;
}

public String getClosingdate() {
return closingDate;
}

public void setClosingdate(String closingDate) {
this.closingDate = closingDate;
}

public List<Values> getValues() {
return values;
}

public void setValues(List<Values> values) {
this.values = values;
}

public List<Instances> getInstances() {
return instances;
}

public void setInstances(List<Instances> instances) {
this.instances = instances;
}

public List<BranchCustomers> getBranchCustomers() {
return branchCustomers;
}

public void setBranchCustomers(List<BranchCustomers> branchCustomers) {
this.branchCustomers = branchCustomers;
}
}
public class BranchCustomers {

private byte branchId;
private long customerNumber;
private List<Products> products;

public byte getBranchid() {
return branchId;
}

public void setBranchid(byte branchId) {
this.branchId = branchId;
}

public long getCustomernumber() {
return customerNumber;
}

public void setCustomernumber(long customerNumber) {
this.customerNumber = customerNumber;
}

public List<Products> getProducts() {
return products;
}

public void setProducts(List<Products> products) {
this.products = products;
}
}
public void parseJson(String response) {
try{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Response result = mapper.readValue(response, Response.class);
System.out.println(result);

} catch (JsonMappingException e) {
e.printStackTrace
} catch (JsonProcessingException e) {
e.printStackTrace();
}

其他 DTO 类“Values”、“Instances”和“Products”此时为空,但我还不需要它们。问题是“结果”值具有正确的结构和正确的字段,但它们是 null 或 0,具体取决于它们的数据类型,而且我看不到我做错了什么。

有人可以帮助我吗?

最佳答案

您的 getter、setter 名称不正确。

举个例子,在您的 Response 对象中,您有 getProductidgetBranchidgetCustomernumber 以及类似的内容 setter 方法。但是,您的 JSON 包含 productIdbranchIdcustomerNumber。它们有所不同,因为您没有使用正确的大小写。您应该使用驼峰命名法,这会将您的命名转换为 getProductIdgetBranchIdgetCustomerNumber。然后,它会匹配 JSON,并进行相应的映射。

这个:

public class Response {

private String partnerId;
private byte branchId;
private long customerNumber;
private Long subAccountNumber;
private Byte productType;
private String openingDate;
private String closingDate;
private List<BranchCustomers> branchCustomers;


public String getProductId() {
return partnerId;
}

public void setProductId(String productid) {
this.partnerId = productid;
}

public byte getBranchId() {
return branchId;
}

public void setBranchId(byte branchid) {
this.branchId = branchid;
}

public long getCustomerNumber() {
return customerNumber;
}

public void setCustomerNumber(long customernumber) {
this.customerNumber = customernumber;
}
...

导致:

Response{productId='NDAtOS0wLS0=', branchId=5, customerNumber=240 ...

作为旁注,我建议使用 IDE 自动生成样板代码,或者查看 Project Lombok只需添加注释即可生成样板代码。这也可以防止您犯命名错误。

此外,尝试为每个 pojo 获取一个 toString 方法,因为它将在日志记录或调试期间为您提供帮助。

关于java - 在Java中解析JSON返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60134943/

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