我正在尝试将数据从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;
}
答案 0 :(得分:0)
你可以发布链接获取json或你的json。如果可以,你应该尝试使用gson库来解析json,例如:gson example
答案 1 :(得分:0)
你应该编写类似这样的代码。
resultStrs[i] = rule;
这将解决您的问题。 欢呼声。