【问题标题】:Volley JSONObject request Parsing Response returns No Value forVolley JSONObject 请求解析响应返回无值
【发布时间】:2019-04-09 10:51:02
【问题描述】:

我有一个 RESTful API 在我的本地机器上运行,它根据请求返回 JSON 响应(实际上这个 JSON 是 nodejs Soap 客户端请求的响应)。对于这种特殊情况,我收到来自 Android 客户端的 POST 请求并返回以下响应:

{
    QueryAcctBalResponse: {
        BalExDtoList: {
            BalExDto: [{
                BalID: "xxxx",
                AcctResID: "xxxx",
                AcctResName: "xxxx",
                BalType: "xxxx",
                Balance: "xxxx",
                EffDate: "xxxx",
                ExpDate: "xxxx",
                UpdateDate: "xxxx"
            }, {
                BalID: "yyyy",
                AcctResID: "yyyy",
                AcctResName: "yyyy",
                BalType: "yyyy",
                Balance: "yyyy",
                EffDate: "yyyy",
                ExpDate: "yyyy",
                UpdateDate: "yyyy"
            }]
        }
    }
}

问题是每次我尝试解析该响应并在 android 中显示此信息(特别是“AcctResName”)。我得到 org.json.JSONException: BalExDto 没有价值。我在 android 中使用 Volley Libray 来解析 Json,我使用了 Jsonobject 请求。

请求代码。

JsonObjectRequest sq = new JsonObjectRequest(Request.Method.POST, balanceUrl, request, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {

        try {
            JSONArray jsonArray = response.getJSONArray("BalExDto");
            for (int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                Toast.makeText(CheckBalance.this, ""+jsonObject.get("AcctResName"), Toast.LENGTH_LONG).show();
            }

            pd.hide();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d("Response", "Response_TAG: "+response);

    }

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

    这是因为你没有 JSON 数组,你有一个 JSON 对象。

    首先,您需要从您的 API 响应中获取 JSON 对象,

    JSONObject jsonObject = response.getJSONObject("QueryAcctBalResponse")
    

    JSON 对象内部是 JSON 数组。

    JSONArray jsonArray = response.getJSONArray("BalExDtoList");
    

    【讨论】:

      【解决方案2】:

      错误说 - 你正在解析 BalExDto 错误。 BalExDto 不是 response 的直接子代。所以你必须解析QueryAcctBalResponseBalExDtoList,然后你才能得到BalExDto。因为BalExDtoBalExDtoList 内部,而BalExDtoListQueryAcctBalResponse 内部。

      JsonObjectRequest sq = new JsonObjectRequest(Request.Method.POST, balanceUrl, request, new Response.Listener<JSONObject>() {
      
      @Override
      public void onResponse(JSONObject response) {
      
          try {
              JSONObject queryAcctBalResponse = response.getJSONObject("QueryAcctBalResponse");
              JSONObject balExDtoList = queryAcctBalResponse.getJSONObject("BalExDtoList");
              JSONArray jsonArray = balExDtoList.getJSONArray("BalExDto");
              for (int i = 0; i < jsonArray.length(); i++){
                  JSONObject jsonObject=jsonArray.getJSONObject(i);
                  Toast.makeText(CheckBalance.this, ""+jsonObject.get("AcctResName"), Toast.LENGTH_LONG).show();
              }
      
              pd.hide();
          } catch (JSONException e) {
              e.printStackTrace();
          }
          Log.d("Response", "Response_TAG: "+response);
      
      }
      

      【讨论】:

        【解决方案3】:

        BalExDto 数组不是响应的直接子代。它在BalExDtoList 内部,在QueryAcctBalResponseobject 内部。您不能直接访问 JSON 中的任何节点,您需要遍历完整路径才能访问它们。

        你需要得到如下的数组

        JSONObject queryAcctBalResponse = response.getJSONObject("QueryAcctBalResponse");
        JSONObject balExDtoList = queryAcctBalResponse.getJSONObject("BalExDtoList");
        JSONArray jsonArray = balExDtoList.getJSONArray("BalExDto");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-08
          • 1970-01-01
          • 1970-01-01
          • 2018-01-01
          • 2022-01-10
          • 2018-12-18
          • 1970-01-01
          • 2015-09-26
          相关资源
          最近更新 更多