Java 为什么我没有得到jsonObject.getString(“天气”)的任何输出? 受保护的void onPostExecute(字符串s) { super.onPostExecute(s); log.i(“URL”,s)//我正在获取此部件的输出 尝试 { JSONObject JSONObject=新的JSONObject;//已创建JSONObject String weatherInfo=jsonObject.getString(“天气”);//从天气部分获取信息 Log.i(“JSON”,urlResult);//但不适用于此部分 JSONArray JSONArray=新JSONArray(天气信息); for(int i=0;i
我正在尝试从openworldmap.org获取天气信息。我正在使用Android Studio 3.0。我传递到代码中的URL是您可以调用getJSONArrayJava 为什么我没有得到jsonObject.getString(“天气”)的任何输出? 受保护的void onPostExecute(字符串s) { super.onPostExecute(s); log.i(“URL”,s)//我正在获取此部件的输出 尝试 { JSONObject JSONObject=新的JSONObject;//已创建JSONObject String weatherInfo=jsonObject.getString(“天气”);//从天气部分获取信息 Log.i(“JSON”,urlResult);//但不适用于此部分 JSONArray JSONArray=新JSONArray(天气信息); for(int i=0;i,java,android,json,Java,Android,Json,我正在尝试从openworldmap.org获取天气信息。我正在使用Android Studio 3.0。我传递到代码中的URL是您可以调用getJSONArray protected void onPostExecute(String s) { super.onPostExecute(s); log.i("URL" , s) // I am getting output for this part try
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
}
}
在for循环中,应该使用get(i)
我用链接的字符串运行了这段代码,它正确地输出了“main”和“description”。你记录的结果是什么,不是应该是“天气信息”吗?是的,是我的错。但是传递weatherInfo也没有给我任何结果它仍然不起作用,W/System.err:org.json.JSONException:org.json.JSONObject$1类型的值null不能在org.json.json.json.typemisch(json.java:111)在org.json.JSONObject(JSONObject.java:163)在org.json.JSONObject(JSONObject.java:176)转换为JSONObject这就是错误,我是getting@SanketGanorkar,访问数组中某个项的方法有错误,请使用jsonArray.get(i),
//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");
JSONObject jsonPart = jsonArray.get(i); //jsonArray.getJSONObject(i);