ShivLeela
ShivLeela

Reputation: 117

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 there any method to parse JSON bundle or should I convert into JSON object and then parse it?

Below is my response from paytm in JSON format.

Merchant Response is {"MID":"SAMPLE09557238310462","ORDERID":"ORDER20000995","TXNAMOUNT":"171.75","CURRENCY":"INR","TXNID":"612917","BANKTXNID":"154301","STATUS":"TXN_SUCCESS","RESPCODE":"01","RESPMSG":"Txn Successful.","TXNDATE":"2016-03-02 16:38:12.0","GATEWAYNAME":"WALLET","BANKNAME":"","PAYMENTMODE":"PPI","IS_CHECKSUM_VALID":"Y"}

This is what I have tried.

 public void onTransactionSuccess(Bundle inResponse) {
                        // After successful transaction this method gets called.
                        // // Response bundle contains the merchant response
                        // parameters.
                        Log.d("LOG", "Payment Transaction is successful " + inResponse);
                        Toast.makeText(getApplicationContext(), "Payment Transaction is successful", Toast.LENGTH_LONG).show();
                        try {
                            JSONObject json = new JSONObject( inResponse.getString("") );
                            JSONObject json2 = json.getJSONObject("");
                            String name1 = (String) json2.get("ORDERID");
                            Toast.makeText(getApplicationContext(), ""+name1, Toast.LENGTH_LONG).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

Upvotes: 1

Views: 3854

Answers (3)

Momen Zaqout
Momen Zaqout

Reputation: 1498

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 = new JSONObject();
    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 = new Gson();
        Type type = new TypeToken<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 instanceof String) {
                            bundle.putString(key, (String) value);
                        } else if (value instanceof Integer) {
                            bundle.putInt(key, (Integer) value);
                        } else if (value instanceof Boolean) {
                            bundle.putBoolean(key, (Boolean) value);
                        } else if (value instanceof Double) {
                            bundle.putDouble(key, (Double) value);
                        } else if (value instanceof Float) {
                            bundle.putFloat(key, (Float) value);
                        } else if (value instanceof Long) {
                            bundle.putLong(key, (Long) value);
                        }
// if you have any other types, add it
                    }


            } catch (JSONException e) {
                e.printStackTrace();
            }

Upvotes: 0

DoTx
DoTx

Reputation: 41

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

public void onTransactionSuccess(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.

Upvotes: 1

Vivek Bhardwaj
Vivek Bhardwaj

Reputation: 528

do one thing ! You can get this merchant response like this

@Override
    public void onActivityResult(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 = new JSONObject(confirm.toJSONObject().toString());
                            String paymentID = jobj.getJSONObject("response").getString("id");


                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                    } 
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            else if(requestCode  == Activity.RESULT_CANCELED)
            {
                Log.e("Paypal ", "User Cancelled THe Process");
                Toast.makeText(context, "You cancelled the transaction", Toast.LENGTH_LONG).show();
            }
            else if(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! :)

Upvotes: 0

Related Questions