gpt4 book ai didi

java - 为什么我没有得到 jsonObject.getString ("weather"的任何输出?

转载 作者:行者123 更新时间:2023-12-02 08:46:46 25 4
gpt4 key购买 nike

 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

最佳答案

你可以调用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");

在 for 循环中你应该使用 get(i);

JSONObject jsonPart = jsonArray.get(i); //jsonArray.getJSONObject(i);

关于java - 为什么我没有得到 jsonObject.getString ("weather"的任何输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61029419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com