How To Convert Json Bundle To Json Object In Android
Hi In my App I have Integrated Paytm Wallet and I'm getting the response as a JSON Bundle object type. Now I need to get the details from that JSON like ORDERID,TXNAMNT etc., Is th
Solution 1:
I don't really know much about the PayTM Android API. But I believe it would be possible that the Bundle will contain the information itself rather than holding a JSON.
eg. Try the following inside the OnTransactionSucess function
publicvoidonTransactionSuccess(Bundle inResponse) {
String mid = inResponse.getString("MID");
String orderId = inResponse.getString("ORDERID");
// Do stuff with your information
}
You can check the values using Log.d.
I hope this helps.
Solution 2:
do one thing ! You can get this merchant response like this
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null)
{
try
{
try
{
Log.e("Paypal Response ", confirm.toJSONObject().toString());
JSONObject jobj = newJSONObject(confirm.toJSONObject().toString());
String paymentID = jobj.getJSONObject("response").getString("id");
}
catch(Exception e)
{
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
elseif(requestCode == Activity.RESULT_CANCELED)
{
Log.e("Paypal ", "User Cancelled THe Process");
Toast.makeText(context, "You cancelled the transaction", Toast.LENGTH_LONG).show();
}
elseif(requestCode == PaymentActivity.RESULT_EXTRAS_INVALID)
{
Log.e("Paypal ", "Invalid Payment Requested");
Toast.makeText(context, "Invalid Payment Requested", Toast.LENGTH_LONG).show();
}
}
}
Let me know if this works! :)
Solution 3:
We can achieve this by
1- convert bundle to JSONOBJECT and save it
2- we can also convert back JSONOBJECT to bundle
convert bundle to JSONOBJECT
JSONObject jsonObject = newJSONObject();
Set<String> keys = bundle.keySet();
for (String key : keys) {
try {
jsonObject.put(key, bundle.get(key));
} catch(JSONException e) {
//Handle exception here
}
}
convert JSONOBJECT to String so we can save it
Gson gson = newGson();
Typetype = newTypeToken<String>() {
}.getType();
String jsonString= gson.fromJson(jsonObject, type);
convert JSONOBJECT to bundle
try {
Bundle bundle = new Bundle();
JSONObject jsonObject = new JSONObject(jsonString);
Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = jsonObject.get(key);
if (value instanceofString) {
bundle.putString(key, (String) value);
} elseif (value instanceofInteger) {
bundle.putInt(key, (Integer) value);
} elseif (value instanceofBoolean) {
bundle.putBoolean(key, (Boolean) value);
} elseif (value instanceofDouble) {
bundle.putDouble(key, (Double) value);
} elseif (value instanceofFloat) {
bundle.putFloat(key, (Float) value);
} elseif (value instanceof Long) {
bundle.putLong(key, (Long) value);
}
// if you have any other types, add it
}
} catch (JSONException e) {
e.printStackTrace();
}
Post a Comment for "How To Convert Json Bundle To Json Object In Android"