2015-02-06 86 views
-2

我想從數組中返回數據,因此我可以通過它們循環。之後對udacity進行了android開發培訓,但培訓中使用的JSON結構與我正在使用的不同。以下是我的課我如何返回JSONObject數據作爲數組

private String[] getWeatherDataFromJson(String forecastJsonStr) 
      throws JSONException { 

     // These are the names of the JSON objects that need to be extracted. 
     final String OWM_LIST = "rules"; 
     final String OWM_DESCRIPTION = "game_rules_content"; 

     JSONObject forecastJson = new JSONObject(forecastJsonStr); 
     JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); 
     Log.v(LOG_TAG, "success: " + forecastJson.getInt("success")); 

     //I guess my problem is starts here 
     String[] resultStrs = new String[1]; 

     for(int i = 0; i < weatherArray.length(); i++) { 

      String rule; 
      // Get the JSON object representing the day 
      JSONObject dayForecast = weatherArray.getJSONObject(i); 
      rule = dayForecast.getString(OWM_DESCRIPTION); 
      Log.v(LOG_TAG, "sammy: " + rule); 

      // how do i return my json data as array for i loop through the array with for 
      resultStrs[0] = rule; 

     } 

     for (String s : resultStrs) { 
      Log.v(LOG_TAG, "Forecast entry: " + s); 
     } 
     return resultStrs; 

    } 

回答

0

你可以發佈鏈接獲取json或你的json。如果你可以嘗試使用gson庫來解析json,例如:gson example

0

你應該這樣編碼。

resultStrs[i] = rule; 

這將解決您的問題。 乾杯。