【发布时间】:2015-02-06 10:03:03
【问题描述】:
我正在尝试将 JSONObject 中的数据作为数组返回,以便循环遍历它们。在 udacity 上进行了 android 开发培训,但培训中使用的 JSON 结构与我使用的不同。下面是我的课
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;
}
【问题讨论】: