Hi Guys can anyone help me to parse the following json response.
{
"result": "success",
"reason": "Operation completed successfully.",
"api": "authenticate_inmate",
"locked": false,
"payload": {
"authenticated": true,
"inmate_details": {
"jail_alt_id": "9584",
"facility_number": "1047",
"last_name": "ARMSTRONG",
"first_name": "DONALD",
"dob": "19601213",
"middle_name": "LEROY",
"sex": "M",
"account_frozen": "N",
"balance": "122.73",
"pin": "9584",
"location": "HU H 121B",
"macs_menu_id": "1047-2000000369",
"indigent": 0
},
"order_id": "5d1f7c10-833f-0139-4675-3c97e15872c0",
"order_detail": [{
"id": null,
"order_id": "5d1f7c10-833f-0139-4675-3c97e15872c0",
"product_code": "5010",
"quantity": 1
}],
"transaction_history": "\"1/25/21\",\"1.12\",\"Commissary (100301)\"\r\n\"1/25/21\",\"2.11\",\"Commissary (100301)\"\r\n\"1/25/21\",\"1.41\",\"Commissary (100301)\"\r\n\"1/25/21\",\"0.34\",\"Commissary (100301)\"\r\n\"1/25/21\",\"1.41\",\"Commissary (100301)\"\r\n\"1/22/21\",\"16.00\",\"Commissary (100301)\"\r\n\"12/22/20\",\"10.00\",\"Depositor: Testing LastName\"\r\n\"12/21/20\",\"10.00\",\"Deposit Holding Account (100199)\"\r\n\"11/20/20\",\"-1.00\",\"Commissary (100301)\"\r\n\"11/06/20\",\"5.00\",\"Deposit Holding Account (100199)\"\r\n"
}
}
I want to get inmate_details, Order id, order details & transaction history. i need to show those details in individual screen. i generated model class from json online & i tried in retrofit, but i am getting the response as null. i added my code below, thanks
apIservice=ApiClient.getClient().create(APIService.class);
call=apIservice.checkLogin(inmate_id,pin);
call.enqueue(new Callback<Payload>() {
@Override
public void onResponse(Call<Payload> call, Response<Payload> response) {
System.out.println(response);
if (response.isSuccessful()){
Inmate_details balance = response.body().getInmate_details();
pDialog.dismissWithAnimation();
Toasty.success(LoginActivity.this, "Success! Redirecting To Home", Toast.LENGTH_SHORT, true).show();
startActivity(new Intent(LoginActivity.this,PDFActivity.class));
}
}
private fun getRetrofitObject() : Retrofit {
retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("Api Link")
.build()
return retrofit
}
//Calling API Add User
private fun callAddUserAPI() {
val apiInterface = getRetrofitObject().create(APIInterface::class.java)
val userRequest = AddUserRequest(editTextName.text.toString(), editTextEmail.text.toString())
val addUserAPI = apiInterface.addUserAPI(userRequest)
addUserAPI.enqueue(object : Callback<AddUserResponse> {
override fun onResponse(call: Call<AddUserResponse>, request: Response<AddUserResponse>) {
val body = request.body()
Log.i(DATA, body?.name.toString())
}
override fun onFailure(call: Call<AddUserResponse>, t: Throwable) {
Log.i(DATA, FAILURE_MESSAGE)
}
})
}
Try this, maybe this will hepful.
Parse json
like below:
// ResponseObject is your root object
public class ResponseObject{
@SerializedName("payload")
private Payload payload;
public Payload getPayload() {
return payload;
}
}
class Payload{
@SerializedName("authenticated")
private boolean isAuthenticated;
@SerializedName("inmate_details")
private InMateObject inMateObject;
@SerializedName("order_id")
private String orderId;
@SerializedName("order_detail")
private List<Order> orderList;
// Make sure to get String value as response for "transaction_history" key
@SerializedName("transaction_history")
private String transactionDetails;
public InMateObject getInMateObject() {
return inMateObject;
}
}
class InMateObject{
@SerializedName("jail_alt_id")
private String jainAltId;
@SerializedName("facility_number")
private String facilityNumber;
@SerializedName("last_name")
private String lastName;
@SerializedName("first_name")
private String firstName;
@SerializedName("dob")
private String dob;
@SerializedName("middle_name")
private String middleName;
@SerializedName("sex")
private String sex;
// Add other fields like wise.
}
class Order{
@SerializedName("id") // If id is Integer in json then use int type
private String id;
@SerializedName("order_id")
private String orderId;
@SerializedName("product_code")
private String productCode;
@SerializedName("quantity")
private int quantity;
}
And then your Api
call would look like below:
call.enqueue(new Callback<ResponseObject>() {
@Override
public void onResponse(Call<ResponseObject> call, Response<ResponseObject> response) {
System.out.println(response);
if (response.isSuccessful()){
//Inmate_details balance = response.body().getInmate_details();
// Basically you are getting ResponseObject as response. So, collect payload first from it. And then collect inmate_details from Payload.
Payload payload = response.body().getPayload();
InMateObject inMateObject = payload.getInMateObject();
pDialog.dismissWithAnimation();
Toasty.success(LoginActivity.this, "Success! Redirecting To Home", Toast.LENGTH_SHORT, true).show();
startActivity(new Intent(LoginActivity.this,PDFActivity.class));
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.