2017-05-28 193 views
0

所以我想從一個JSONArray的值到一個JSONObject,但我不斷得到這個錯誤: org.json.JSONException:Value Aamod守基在java.lang.String類型0不能轉換到的JSONObjectorg.json.JSONException:Valuef類型java.lang.String不能轉換爲JSONObject

JSONObject json = jParser.makeHttpRequest(url_all_resorts, "POST", params); 
      Log.d("All Resorts : ", json.toString()); 
      // Checking for SUCCESS TAG 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // resorts found 
       // Getting Array of resorts 
       resorts = json.getJSONArray(TAG_RESORT); 
       rates = json.getJSONArray(TAG_PRICE); 

       int i; 

       // looping through All resorts 
       for (i = 0; i < resorts.length() & i < rates.length(); i++) { 
        JSONObject c = resorts.getJSONObject(i); 
        JSONObject d = rates.getJSONObject(i); 

        // Storing each json item in variable 
        String name = c.getString(TAG_NAME); 
        String price = d.getString(TAG_ROOM_PRICE); 

        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put(TAG_NAME, name); 
        map.put(TAG_ROOM_PRICE, price); 

        // adding HashList to ArrayList 
        resortsList.add(map); 
       } 

      } 

JSON樣本數據

{"resorts":["Aamod Shoghi","Aamod Shoghi","Aamod Shoghi"],"room_prices":["100","800","1000"],"success":1} 
+1

安置自己的json的樣本數據 – FAT

+0

添加了數據@FAT –

回答

1

resortsroom_prices陣列只包含strings

用途:

// Storing each json item in variable 
String name = resorts.getString(i); 
String price = rates.getString(i); 

相反的:

JSONObject c = resorts.getJSONObject(i); 
JSONObject d = rates.getJSONObject(i); 

// Storing each json item in variable 
String name = c.getString(TAG_NAME); 
String price = d.getString(TAG_ROOM_PRICE); 

試試這個:

 ........... 
     ................... 

     if (success == 1) { 
      // resorts found 
      // Getting Array of resorts 
      resorts = json.getJSONArray("resorts"); 
      rates = json.getJSONArray("room_prices"); 

      int i; 

      // looping through All resorts 
      for (i = 0; i < resorts.length() & i < rates.length(); i++) { 
       // Storing each json item in variable 
       String name = resorts.getString(i); 
       String price = rates.getString(i); 

       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map.put(TAG_NAME, name); 
       map.put(TAG_ROOM_PRICE, price); 

       // adding HashList to ArrayList 
       resortsList.add(map); 
      } 

     } 
+0

謝謝!它的工作@FAT –

+0

最受歡迎:) – FAT

相關問題