Issue
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());
Solution
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.
Answered By - navis1692
Answer Checked By - Katrina (JavaFixing Volunteer)