[英]org.json.JSONException: JSONObject[“weather”] is not a string
I am currently using a weather API from https://openweathermap.org/api and I'm trying to convert a JSON page into printable strings.我目前正在使用来自https://openweathermap.org/api的天气 API 并且我正在尝试将 JSON 页面转换为可打印的字符串。 To be more specific.更加具体。 Im tryying to print out the Array of "weather" as the key but its not working:我试图打印出“天气”数组作为键,但它不起作用:
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws ParseException, org.json.simple.parser.ParseException {
String URL = "http://api.openweathermap.org/data/2.5/weather?q=miami&appid=2550e0628a9e7428e4ef85626feb1c95";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet getURL = new HttpGet(URL);
try {
CloseableHttpResponse response1 = httpclient.execute(getURL);
HttpEntity entity = response1.getEntity();
JSONObject json = new JSONObject(EntityUtils.toString(entity));
json.getString("weather");
} catch (IOException e) {
System.out.println("Failed");
}
}
}
The JSONObject structure is this: JSONObject 结构是这样的:
"coord": {
"lon": -80.19,
"lat": 25.77
},
"weather": [{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}],
"base": "stations",
"main": {
"temp": 301.17,
"feels_like": 303.16,
"temp_min": 300.93,
"temp_max": 301.48,
"pressure": 1018,
"humidity": 74
},
"visibility": 16093,
"wind": {
"speed": 4.6,
"deg": 80
},
"clouds": {
"all": 90
},
"dt": 1591061650,
"sys": {
"type": 1,
"id": 4896,
"country": "US",
"sunrise": 1591007368,
"sunset": 1591056495
},
"timezone": -14400,
"id": 4164138,
"name": "Miami",
"cod": 200
}
The error:错误:
at org.json.JSONObject.wrongValueFormatException(JSONObject.java:2590)
at org.json.JSONObject.getString(JSONObject.java:863)
at Main.main(Main.java:23)
Im trying to get the "weather" section of the JSON or any sub section of the JSON.我试图获取 JSON 的“天气”部分或 JSON 的任何子部分。 Could someone help me with my code?有人可以帮我写代码吗?
json.getString(key);
Returns a string if
the value of the specified key
is a String
. if
指定key
的值为 String ,则返回一个String
。
In your case, the value of weather
is a JSONArray
and not a String
.在您的情况下, weather
的值是JSONArray
而不是String
。
You should do:你应该做:
json.getJSONArray("weather");
Ideally, you should check if the key exists, or have a default value to avoid NPE
.理想情况下,您应该检查密钥是否存在,或者有一个默认值以避免NPE
。
if (json.has("weather") && json.get("weather") instanceof JSONArray) {
json.getJSONArray("weather");
}
Unless you are 100% sure of the JSON
structure.除非您 100% 确定JSON
结构。
You should use getString
for string values, if its boolean you should use getBoolean
if decimal getDouble
or something..你应该使用getString
作为字符串值,如果它的 boolean 你应该使用getBoolean
如果十进制getDouble
什么的..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.