0

I am trying to parse a JSON with Java and I do not get the expected result. Example 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": []
}

Not all the JSONs have this structure, some may miss some of the fields. I created some DTO classes for parsing it. This is my code:

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();
}

The other DTO classes "Values", "Instances" and "Products" are empty at this moment, but I do not need them yet. The problem is that the "result" value has the right structure and the right fields, but they are null or 0, depending on their data type and I cannot see what I have done wrong.

Can anyone help me?

2
  • Post your error logs here. Also post your json. Commented Feb 9, 2020 at 9:08
  • Also it looks like instances and values doesn't belong to the same parent from your json. Commented Feb 9, 2020 at 9:12

1 Answer 1

1

Your getters, setters have incorrect names.

As an example, in your Response object, you have getProductid, getBranchid, and getCustomernumber with similar setter methods. Your JSON however has productId, branchId, and customerNumber. They differ as you didn't use the correct capitalization. You should use camelCase, which would turn your naming to getProductId, getBranchId, and getCustomerNumber. This then matches the JSON and it will map accordingly.

This:

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;
}
...

Leads to:

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

As a sidenote, I suggest either auto-generating the boilerplate code with your IDE or look into Project Lombok which can generate the boilerplate code by just adding an annotation. This also stops you from making naming mistakes.

Also, try and get a toString method for each pojo, as it will help you during logging or debugging.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, the getters and setters were the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.