如何从json数组中检索数据

时间:2014-04-09 12:24:35

标签: android json parsing

这是我的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_);

3 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

gson是json解析的另一个完美解决方案。

答案 2 :(得分:0)

事务不是JsonArray。那就是JsonObject。试试下面的代码。我希望这会对你有所帮助

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