2014-04-09 59 views
2

這是我的JSON響應看起來像如何從一個JSON數組

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

,當我嘗試從每個參數高達狀態,我可以提取它,但是當我嘗試提取CURRENCYCODE提取值檢索數據它顯示No value for currencyCode

我看到this職位和修改我的代碼,但沒有使用但

我的代碼

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_); 

回答

4

試試這個..

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

刪除以下行

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

並添加以下行

 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"); 
+0

它的工作,但現在得到總值沒有值。我試過 AppVariables.total = jObject.getJSONObject(「total」)。toString(); AppVariables.qrcode = jObject.getJSONObject(AppVariables.qrcode _)。toString(); 但似乎沒有工作 – suja

+0

@suja你必須得到像'AppVariables.total = jObject.getString(「total」))''。 – Hariharan

+0

我試過,但它仍然顯示我沒有價值的qrcode .. – suja

0

gson是JSON解析另一個完美的解決方案。

0

交易不是JsonArray。這是JsonObject.Try以下代碼。我希望這會幫助你

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");