如何在android中将JSON包转换为JSON对象

时间:2016-03-02 11:48:33

标签: android json

您好,在我的应用程序中,我使用了集成Paytm钱包,并且我将响应作为JSON Bundle对象类型获得。现在我需要从ORSONID,TXNAMNT等JSON获取详细信息,是否有任何解析JSON包的方法或者我应该转换为JSON对象然后解析它?

以下是我对来自paymm的JSON格式的回复。

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"}

这就是我的尝试。

 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();
                        }
                    }

3 个答案:

答案 0 :(得分:0)

做一件事!您可以像这样得到此商家回复

@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();
            }
        }
    }

让我知道这是否有效! :)

答案 1 :(得分:0)

我对PayTM Android API并不是很了解。但我相信Bundle可能会包含信息而不是持有JSON。

例如。在OnTransactionSucess函数中尝试以下内容

public void onTransactionSuccess(Bundle inResponse) {
    String mid = inResponse.getString("MID");
    String orderId = inResponse.getString("ORDERID");

    // Do stuff with your information
}

您可以使用Log.d。

检查值

我希望这会有所帮助。

答案 2 :(得分:0)

我们可以通过

1- 将 bundle 转换为 JSONOBJECT 并保存

2- 我们也可以将 JSONOBJECT 转换回 bundle

<块引用>

将捆绑包转换为 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
        }
    }
<块引用>

将 JSONOBJECT 转换为 String 以便我们可以保存它

 Gson gson = new Gson();
        Type type = new TypeToken<String>() {
        }.getType();
    
        String jsonString= gson.fromJson(jsonObject, type);
<块引用>

将 JSONOBJECT 转换为包

    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();
            }