2

I am trying to convert my JSONObject object into java objects but continuously getting null values only in my java object. Seeking your suggestions that how can i set my json values as java object so that i can save those java objects in my hibernate entities. It would be greate if you could tell me how to create my java POJO according to this json.

Below is my json :

{
  "msg": "1 out of 1 Transactions Fetched Successfully",
  "transaction_details": {
    "148042": {
      "firstname": "Navneet",
      "transaction_amount": "11.00",
      "amt": "11.00",
      "Settled_At": "0000-00-00 00:00:00",
      "addedon": "2018-09-26 20:36:25",
      "mode": "CC",
      "card_no": "512345XXXXXX2346",
      "additional_charges": "0.00",
      "error_Message": "NO ERROR",
      "payment_source": "payu",
      "bank_ref_num": "368960",
      "bankcode": "CC",
      "txnid": "148042",
      "unmappedstatus": "captured",
      "udf5": null,
      "mihpayid": "40399371551645689",
      "udf3": null,
      "udf4": null,
      "net_amount_debit": 11,
      "udf1": null,
      "card_type": "MAST",
      "udf2": null,
      "Merchant_UTR": null,
      "field9": " Verification of Secure Hash Failed: E700 -- Approved -- Transaction Successful -- Unable to be determined--E000",
      "error_code": "E000",
      "disc": "0.00",
      "productinfo": "Health",
      "request_id": "",
      "field2": "178707",
      "PG_TYPE": "AXISPG",
      "name_on_card": "Mukesh",
      "status": "success"
    }
  },
  "status": 1
}

Here is my java code to convert json String into java objects:

 if(response.getStatusCode().toString().equalsIgnoreCase("200")){
                    String responseString = response.getBody();
                    JSONObject responseJson = new JSONObject(responseString);
                     output=new com.google.gson.Gson().toJson(responseJson);
                     ObjectMapper objectMapper  = new ObjectMapper();
                     objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

                     PayUTransactionMap payutxnmap=objectMapper.readValue(responseString, PayUTransactionMap.class);

                     PayUTransactionMap obj = new Gson().fromJson(output, PayUTransactionMap.class);
                   transaction.setPgBankRefNo(payutxnmap.getBank_ref_num());
                     transaction.setPgTxnRefNo(payutxnmap.getMihpayid());
                     transaction.setPgTxnNo(payutxnmap.getBank_ref_num());
                     transaction.setTxnMode(payutxnmap.getBankcode());
1
  • can u share your java class that you want this JSON to set to Commented Oct 26, 2018 at 16:34

2 Answers 2

1

As per the above json you need to create 3 java classes :

class1

private String status;
private String msg;
private Class2 transactionDetails;

Class2

private Class3 transactionparameters;

Class3

public String firstname;
public String transaction_amount;
public String amt;
public String Settled_At;
public String addedon;
     etc etc

Now for accessing the objects you can use

class1_Object.getTransactionDetails().getTransactionparameters();

this will fetch you all the details available in 3rd class.

I hope this will resolve your problem.

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

Comments

0

That JSON data can be mapped into 2 classes:

class Root {
    int status;
    String msg;
    Map<String, Detail> transaction_details;
}

class Detail {
    String firstname;
    String name_on_card;
    BigDecimal amt;
    // and many more
}

Remember, JSON properties are unordered, so you can order the fields in a way that makes more sense.

The JSON for transaction_details is a JSON object with dynamic property name, so it needs to be mapped into a Java name-to-value Map.

2 Comments

Hi Andreas, I have tried your way but due to this architecture i am unable to finalize my java POJOs for this json. Can you post java POJOs that will be formed for this kind of json architecture .So that i can check wheter i am getting values in my java objects or not..
@nirmalsharma Those are the Java POJOs for your JSON.

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.