Android JsonObject无法转换为JsonArray

时间:2015-04-21 20:37:32

标签: android arrays json parsing object

这里我有我的JsonArray解析器(我用于其他文件)

JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {

                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());


                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = null;
                            try {
                                obj = response.getJSONObject(i);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }




                            Horaires horaires = new Horaires();

                            JSONObject jObject = obj.getJSONObject("horaires");

                            horaires.setHeure((jObject.getString("heure")));



                            ArrayList<String> listdata = new ArrayList<String>();
                            JSONArray jArray = jObject.getJSONArray("passages");
                            if (jArray != null) {
                                for (int v=0;i<jArray.length();v++){
                                    listdata.add(jArray.get(v).toString());
                                }
                            }

                            horaires.setPassages(listdata);

                            horairesList.add(horaires);


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

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {

        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Log.d("Error", error.getMessage());

            Toast.makeText(getApplicationContext(), "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();


        }


    });

这是我要解析的Json:

{"arret":{"codeArret":"OTAG3","libelle":"50 Otages","accessible":true},"ligne":{"numLigne":"C2","directionSens1":"Le Cardo","directionSens2":"Commerce","accessible":true,"etatTrafic":1,"libelleTrafic":"Service normal"},"codeCouleur":"J","plageDeService":"Des horaires adapt\u00e9s aux rythmes de vos journ\u00e9es\r\nEn p\u00e9riode scolaire...\r\nJour rose : du lundi au vendredi (hors 10 mai).\r\nJour vert : les samedis.\r\nJour bleu : les dimanches et jours f\u00e9ri\u00e9s (aucun service le 1er mai).\r\nPendant les vacances scolaires...\r\nJour jaune : du lundi au vendredi (hors vacances de Toussaint).","horaires":[{"heure":"5h","passages":["19","39","59"]},{"heure":"6h","passages":["19","35","43","51","59"]},{"heure":"7h","passages":["07","16","25","33","41","50","59"]},{"heure":"8h","passages":["07","15","23","32","41","49","59"]},{"heure":"9h","passages":["09","19","30","40","50"]},{"heure":"10h","passages":["00","10","20","30","40","50"]},{"heure":"11h","passages":["00","11","21","31","41","50","59"]},{"heure":"12h","passages":["09","19","29","39","49","59"]},{"heure":"13h","passages":["09","19","29","40","50"]},{"heure":"14h","passages":["00","10","20","30","40","50"]},{"heure":"15h","passages":["00","10","20","29","38","47","56"]},{"heure":"16h","passages":["04","12","20","28","36","44","52"]},{"heure":"17h","passages":["00","07","15","22","30","38","46","54"]},{"heure":"18h","passages":["02","10","18","26","36","45","54"]},{"heure":"19h","passages":["04","14","24","34","44","54"]},{"heure":"20h","passages":["04","18","33","45"]},{"heure":"21h","passages":["00","14","29","44","59"]},{"heure":"22h","passages":["14","34","54"]},{"heure":"23h","passages":["19","49"]},{"heure":"0h","passages":["19"]}],"prochainsHoraires":[{"heure":"22h","passages":["14"]},{"heure":"22h","passages":["34"]}]}

问题是只有一个对象而不是数组,所以我无法解析为JsonArray!我如何重写它以使其工作?

1 个答案:

答案 0 :(得分:1)

检查你的服务器端代码,以及如何在分别捕获JSONObject和JSONArray之后创建数组和对象。

请在此处阅读PHP:JSON