试图从Json对象获取数据

时间:2016-01-03 22:27:05

标签: android android-studio android-volley

我有这种JSON结构:{

-some_name: {
    - data: {
          id: "0ca497ef",
          status: "success"
    },
   - other_name: [
         -{
            id: "id1",
            title: "title1"
         },
         -{
             id: "id2",
             title: "title2"
         } 
    ]
}

我正在尝试使用Volley检索数据,到目前为止我所做的并不起作用:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest (Request.Method.GET, "url", null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {

                                JSONArray jsonArrayresponse = response.getJSONArray("some_name");
                                String[] titles=null;
                                txt = (TextView) findViewById(R.id.name);

                                for (int i=0;i<jsonArrayresponse.length();i++){
                                    JSONObject result = jsonArrayresponse.getJSONObject(i);
                                    JSONArray jsonArrayresult = result.getJSONArray("other_name");

                                    for (int y=0;y<jsonArrayresponse.length();y++) {
                                        String title= result.getString("title");

                                        titles[y] = title;
                                        txt.append(titles[i]);
                                    }
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("VOLLEY", "ERROR");
                        }
                    }
            );

            requestQueue.add(jsonObjectRequest);

所以在这段代码中,我试图获取第一个数组(some_name),然后是第二个数组(other_name),然后将所有标题添加到textView。

有人可以指出我做错了什么,并尝试以简单的方式解释因为我是初学者, 谢谢

1 个答案:

答案 0 :(得分:1)

你标记为

的是什么
  

some_name

不是JSON数组,而是JSON对象。