英文:
Why I am not getting any output for jsonObject.getString("weather")?
问题
protected void onPostExecute(String s) {
super.onPostExecute(s);
log.i("URL", s); // I am getting output for this part
try {
JSONObject jsonObject = new JSONObject(s); // JSONObject Created
String weatherInfo = jsonObject.getString("weather"); // Fetching info from weather section
Log.i("JSON", urlResult); // But not for this part
JSONArray jsonArray = new JSONArray(weatherInfo);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonPart = jsonArray.getJSONObject(i);
// Not getting output for this two lines also
Log.i("main", jsonPart.getString("main")); // fetching info from 'main' section
Log.i("description", jsonPart.getString("description")); // fetching info from 'description' section
}
} catch (Exception e) {
e.printStackTrace(); // Handling Error
}
}
我正在尝试从 openworldmap.org 获取天气信息。我正在使用 Android Studio 3.0。我将以下 URL 传递到代码中:https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22
英文:
protected void onPostExecute(String s)
{
super.onPostExecute(s);
log.i("URL" , s) // I am getting output for this part
try
{
JSONObject jsonObject = new JSONObject(s); // JSONObject Created
String weatherInfo = jsonObject.getString("weather"); // Fetching info from weather section
Log.i("JSON", urlResult); // But not for this part
JSONArray jsonArray = new JSONArray(weatherInfo);
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonPart = jsonArray.getJSONObject(i);
// Not getting output for this two lines also
Log.i("main", jsonPart.getString("main")); // fetching info from 'main' section
Log.i("description", jsonPart.getString("description")); // fetching info from 'description' section
}
}
catch (Exception e)
{
e.printStackTrace(); // Handling Error
}
}
I am trying to fetch weather info from openworldmap.org. I am using Android Studio 3.0. URL that I am passing into code is https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22
答案1
得分: 1
你可以调用 getJSONArray,
//String weatherInfo = jsonObject.getString("weather"); // 从天气部分获取信息
//Log.i("JSON", urlResult); // 但不适用于此部分
//JSONArray jsonArray = new JSONArray(weatherInfo);
JSONArray jsonArray = jsonObject.getJSONArray("weather");
并且在 for 循环中,你应该使用 get(i);
JSONObject jsonPart = jsonArray.get(i); //jsonArray.getJSONObject(i);
英文:
You can call getJSONArray,
//String weatherInfo = jsonObject.getString("weather"); // Fetching info from weather section
//Log.i("JSON", urlResult); // But not for this part
//JSONArray jsonArray = new JSONArray(weatherInfo);
JSONArray jsonArray = jsonObject.getJSONArray("weather");
and in the for loop you should use get(i);
JSONObject jsonPart = jsonArray.get(i); //jsonArray.getJSONObject(i);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论