2014-06-10 3 views
0

Je suis nouveau dans android. J'essaye d'obtenir JSON mais j'ai eu cette erreur. J'essaie de suivre ce tutoriel http://www.androidhive.info/2012/01/android-json-parsing-tutorial/Android JSONObject ne peut pas être converti en JSONArray

Je recherche déjà la solution, mais je ne comprends toujours pas comment utiliser le JSONArray.

Voici mon exemple Json

[{"id":"152","category_id":"14","item_name":"Restaurant1","cuisine_id":"3","cuisine_name":"Chinese"},{"id":"161","category_id":"14","item_name":"Restaurant10","cuisine_id":"17","cuisine_name":"Middle Eastern"},{"id":"153","category_id":"14","item_name":"Restaurant2","cuisine_id":"17","cuisine_name":"Middle Eastern"},{"id":"154","category_id":"14","item_name":"Restaurant3","cuisine_id":"7","cuisine_name":"American"},{"id":"155","category_id":"14","item_name":"Restaurant4","cuisine_id":"3","cuisine_name":"Chinese"},{"id":"156","category_id":"14","item_name":"Restaurant5","cuisine_id":"8","cuisine_name":"Coffee"},{"id":"157","category_id":"14","item_name":"Restaurant6","cuisine_id":"8","cuisine_name":"Coffee"},{"id":"158","category_id":"14","item_name":"Restaurant7","cuisine_id":"17","cuisine_name":"Middle Eastern"},{"id":"159","category_id":"14","item_name":"Restaurant8","cuisine_id":"6","cuisine_name":"Indonesian"},{"id":"160","category_id":"14","item_name":"Restaurant9","cuisine_id":"3","cuisine_name":"Chinese"}] 

Et Ici, la classe

/** 
* Async task class to get json by making HTTP call 
* */ 
private class GetRestaurant extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(Attractions.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     Log.d("Response: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       restaurant = jsonObj.getJSONArray(TAG_ITEM_NAME); 

       // looping through All Contacts 
       for (int i = 0; i < restaurant.length(); i++) { 
        JSONObject c = restaurant.getJSONObject(i); 

        String id = c.getString(TAG_CUISINE_NAME); 

        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_CUISINE_NAME, id); 


        // adding contact to contact list 
        restaurantList.add(contact); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       Attractions.this, restaurantList, 
       R.layout.attractionslayout, new String[] { TAG_ITEM_NAME, TAG_CUISINE_NAME }, new int[] { R.id.item_name, 
         R.id.cuisine_name}); 

    // Assign adapter to ListView 
     listview.setAdapter(adapter); 
    } 

} 

Merci Avant.

Répondre

1

Essayez cette ..

Votre réponse commence par JSONArray

Modifier ce ..

JSONObject jsonObj = new JSONObject(jsonStr); 

// Getting JSON Array node 
restaurant = jsonObj.getJSONArray(TAG_ITEM_NAME); 

à

restaurant = new JSONArray(jsonStr); 
+0

Merci beaucoup. Ça marche. Maintenant, je connais le problème. Donc, si son début avec "{" je peux utiliser mon ancien code? – user3561934

+0

@ user3561934 Ya vous pouvez l'utiliser. – Hariharan

0

Votre chaîne n'est pas objet JSON, c'est jso tableau n parce que la chaîne sont de démarrage via la troisième support

, essayez cette

restaurant = new JSONArray(jsonStr); 

vous n'avez pas besoin de le convertir en un objet josn,

JSONObject jsonObj = new JSONObject(jsonStr); 
Questions connexes