2

this is how my json response looks like

{
    "id": "fe4a69ef-8b8b-42ad-9c5c-9e0c3e449441",
    "createTime": "2014-04-09T11:29:26Z",
    "updateTime": "2014-04-09T11:29:26Z",
    "status": "Created",
    "transaction": {
        "amount": {
            "currencyCode": "GPP",
            "total": 122
        },
        "qrcode": "1f0e3dad99908345f7439f8ffabdfiop",
        "description": "This is the payment transaction description."
    }
}

when I try to extract values from each parameter upto status I could extract it but when I try to extract the currencyCode it shows No value for currencyCode

I saw this post and modified my code but no use yet

my code

JSONObject jObject = new JSONObject(toReturn);

         if(jObject.has("error")){               
             RES_STATUS     =   AppConstants.FAIL;
         }
         else{               
             AppVariables.id                    =   jObject.getString(AppVariables.id_);
             AppVariables.createTime            =   jObject.getString(AppVariables.createTime_);
             AppVariables.updateTime            =   jObject.getString(AppVariables.updateTime_);
             AppVariables.status                =   jObject.getString(AppVariables.status_);
             JSONArray jArray = jObject.getJSONArray("transaction");


             for (int i=0; i < jArray.length(); i++)
             {
                 try {
                     JSONObject oneObject = jArray.getJSONObject(i);
                     // Pulling items from the array
                     String oneObjectsItem = oneObject.getString("total");
                     String oneObjectsItem2 = oneObject.getString("currencyCode");
                     Log.e("oneObjectsItem", oneObjectsItem);
                     Log.e("oneObjectsItem2", oneObjectsItem2);
                 } catch (JSONException e) {
                     // Oops
                     Log.e("OOps", e.toString());
                 }
             }


             AppVariables.total                 =   String.valueOf(jObject.getInt(AppVariables.total_));
             AppVariables.qrcode                =   jObject.getString(AppVariables.qrcode_);

3 Answers 3

4

Try this..

"transaction": {        //this is JSONObject not array

Remove below lines

         JSONArray jArray = jObject.getJSONArray("transaction");
         for (int i=0; i < jArray.length(); i++)
         {
             try {
                 JSONObject oneObject = jArray.getJSONObject(i);
                 // Pulling items from the array
                 String oneObjectsItem = oneObject.getString("total");
                 String oneObjectsItem2 = oneObject.getString("currencyCode");
                 Log.e("oneObjectsItem", oneObjectsItem);
                 Log.e("oneObjectsItem2", oneObjectsItem2);
             } catch (JSONException e) {
                 // Oops
                 Log.e("OOps", e.toString());
             }
         }

And add below lines

        JSONObject transaction_obj = jObject.getJSONObject("transaction");
        JSONObject oneObject = transaction_obj.getJSONObject("amount");
        String oneObjectsItem = oneObject.getString("total");
        String oneObjectsItem2 = oneObject.getString("currencyCode");
        Log.e("oneObjectsItem", oneObjectsItem);
        Log.e("oneObjectsItem2", oneObjectsItem2);
        AppVariables.qrcode = transaction_ob.getString("qrcode");
Sign up to request clarification or add additional context in comments.

2 Comments

It worked but now am getting No value for total. I tried AppVariables.total=jObject.getJSONObject("total").toString(); AppVariables.qrcode=jObject.getJSONObject(AppVariables.qrcode_).toString(); but doesnt seems to work
@suja you have to get like AppVariables.total=jObject.getString("total")).
0

gson is another perfect solution for json resolving.

Comments

0

transaction is not an JsonArray. That is JsonObject.Try following code. I hope this will help you

JSONObject objTransaction= jObject.getJSONObject("transaction");
String strQrcode=objTransaction.getString("qrcode");
String strDescription=objTransaction.getString("description");
JSonObject objAmount=objTransaction.getJSONObject("amount");

String strTotal=objAmount.getString("total");
String strCurrencyCode = objAmount.getString("currencyCode");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.