2014-06-10 2 views
0

저는 android에서 새롭습니다. 나는 json을 얻으려고하고 있지만이 오류가 있습니다. 이 튜토리얼을 따르려고합니다. http://www.androidhive.info/2012/01/android-json-parsing-tutorial/Android JSONObject를 JSONArray로 변환 할 수 없습니다.

이미 솔루션을 검색했지만 아직 JSONArray를 사용하는 방법을 알지 못합니다.

여기

/** 
* 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); 
    } 

} 

감사하기 전에 클래스 여기 내 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"}] 

된다.

답변

1

이 시도 ..

귀하의 응답은

JSONArray

[    ==> JSONArray 
    {   ==> JSONObject 

변경이 ..

JSONObject jsonObj = new JSONObject(jsonStr); 

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

시작

restaurant = new JSONArray(jsonStr); 
+0

덕분에 많이 시도합니다. 그 작품. 이제 나는 그 문제를 안다. 그래서 "{"로 시작한다면 이전 코드를 사용할 수 있습니까? – user3561934

+0

@ user3561934 예. 사용하실 수 있습니다. – Hariharan

0

귀하의 문자열을 JSON 개체가 아닙니다, 그것은 jso이다. n 개의 배열 문자열이

그래서 세 번째 브래킷을 통해 시작되기 때문에, 당신은 josn 객체로 변환 할 필요가 없습니다이

restaurant = new JSONArray(jsonStr); 

,

JSONObject jsonObj = new JSONObject(jsonStr); 
관련 문제