【发布时间】:2020-04-21 21:05:50
【问题描述】:
Error message: com.example.myjson W/System.err: org.json.JSONException: Value
of type org.json.JSONObject cannot be converted to JSONArray
JSON如下
{
“温度”:296.88,
“feels_like”:298.86,
"temp_min":296.88,
"temp_max":296.88,
“压力”:1013,
“湿度”:89,
“海平面”:1013,
"grnd_level":986
}
我可以单独从中获取数据
String weatherInfo = jsonObject.getString("weather");
不是来自这个字符串为什么?
JSONArray jsonArray1 = new JSONArray(weatherInfo1);
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
String weatherInfo = jsonObject.getString("weather");
String weatherInfo1 = jsonObject.getString("main");
Log.i("weatherMainContent", weatherInfo1);
Log.i("Weather Details" , weatherInfo);
JSONArray jsonArray = new JSONArray(weatherInfo);
JSONArray jsonArray1 = new JSONArray(weatherInfo1);
Log.i("full " , jsonArray1.toString());
String message = "";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
String main = jsonObject1.getString("main");
String description = jsonObject1.getString("description");
Log.i("Weather side Details" , weatherInfo);
Log.i("temperaturerrr", jsonObject1.getString("temp_min"));
String temp_min = jsonObject1.getString("temp_min");
Log.i("temperature", jsonObject1.getString("temp_min"));
String pressure = jsonObject1.getString("pressure");
if (!main.equals("") && !description.equals("") && !temp_min.equals("")) {
message += main + ":" + description +";" + temp_min + "\r\n";
} else {
Toast.makeText(getApplicationContext(), "couldn't find the giberish you mentioned :(", Toast.LENGTH_SHORT).show();
}
Log.i("Main", jsonObject1.getString("main"));
Log.i("Description", jsonObject1.getString("temp_min"));
}
if (!message.equals("")) {
resultTextView.setText(message);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "couldn't find the giberish you mentioned :(", Toast.LENGTH_SHORT).show();
}
}
}
【问题讨论】:
-
它不是一个数组。
-
您在 JSONObject 上使用
getString,这将为您提供一个原子字符串。然后将 if 传递给 JSONArray 的构造函数,它需要一个 json 数组作为字符串,但事实并非如此。 -
感谢您的帮助。我是堆栈溢出的新手
-
您的 JSON 与代码中的属性不匹配。确保您发布了正确的 JSON。
-
@Shivas 你想从 jsonarray 中读取数据吗?
标签: java android json android-studio