【问题标题】:android parse jsonObject with volleyandroid用凌空解析jsonObject
【发布时间】:2015-02-16 08:42:53
【问题描述】:

我第一次尝试用 volley 解析 json。我找到了一些例子,我学会了如何使用它。 但我有一个问题...... 我有这样的json

  {
"Items ":
[
{
  "Branch" :"mybranch",
   "City" : "London"
},
 {
  "Branch" :"mybranch1",
   "City" : "Paris"
},

 {
  "Branch" :"mybranch2",
   "City" : "NY"
}


]
}

这是我的解析器代码

private void makeGetDictionaries13Request() {
    showpDialog();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
            urlgetbranchlist, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("TAG", response.toString());

                    try {
                        JSONArray mainjsonArray=response.getJSONArray("");
                        for (int i = 0; i < mainjsonArray.length(); i++) {
                            JSONObject j_object=mainjsonArray.getJSONObject(i);
                            System.out.println();
                            Log.e("City", j_object.getString("City"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                    hidepDialog();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("TAG", "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    // hide the progress dialog
                    hidepDialog();
                }
            });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);

    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
            15000, 
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));    
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}

我可以在日志中显示 jsonobject 结果,但我有凌空错误

no value for

对于我的选择,我在解析儿子时有问题..我做错了什么?

【问题讨论】:

    标签: android json gson android-volley http-get


    【解决方案1】:

    从 JSONObject 响应中获取 JSONArray 存在一个小错误。您实际上忘记提及数组名称items

    错误:

    JSONArray mainjsonArray=response.getJSONArray("");
    

    正确:

    JSONArray mainjsonArray=response.getJSONArray("Items");
    

    【讨论】:

    • 我通过了 json 并且在列表视图中我可以显示项目两次(重复)
    • @chromelend 如果不查看您拥有的代码,我无话可说!
    【解决方案2】:

    使用 Items 而不是 "" 从响应 JSONObject 获取项目 JSONArray

    JSONArray mainjsonArray=response.getJSONArray("Items");
    

    【讨论】:

    • 我通过了 json 并且在列表视图中我可以显示项目两次(重复)
    • @chromelend: 显示你在 ArrayList 中添加数据的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 2017-02-08
    • 2013-11-13
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多