Java 我无法从Json数组中获取数据
JSON如下所示 { “温度”:296.88 “感觉像”:298.86 “最低温度”:296.88 “最高温度”:296.88 “压力”:1013 “湿度”:89 “海平面”:1013 “grnd_级别”:986 } 我可以单独从这里得到数据Java 我无法从Json数组中获取数据,java,android,json,android-studio,Java,Android,Json,Android Studio,JSON如下所示 { “温度”:296.88 “感觉像”:298.86 “最低温度”:296.88 “最高温度”:296.88 “压力”:1013 “湿度”:89 “海平面”:1013 “grnd_级别”:986 } 我可以单独从这里得到数据 Error message: com.example.myjson W/System.err: org.json.JSONException: Value of type org.json.JSONObject cannot be converted
Error message: com.example.myjson W/System.err: org.json.JSONException: Value
of type org.json.JSONObject cannot be converted to JSONArray
不是从这根绳子上为什么
String weatherInfo = jsonObject.getString("weather");
JSONArray jsonArray1=新的JSONArray(weatherInfo1);
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
试一试{
JSONObject JSONObject=新的JSONObject;
字符串weatherInfo=jsonObject.getString(“天气”);
字符串weatherInfo1=jsonObject.getString(“main”);
Log.i(“weatherMainContent”,weatherInfo1);
Log.i(“天气详情”,天气信息);
JSONArray JSONArray=新JSONArray(天气信息);
JSONArray jsonArray1=新JSONArray(weatherInfo1);
Log.i(“full”,jsonArray1.toString());
字符串消息=”;
for(int i=0;i
您的Json不是数组,而是对象。
为Json字符串创建一个新的JSONObject
。
然后只需使用对象并使用getDouble(“propertyName”)
方法来获取属性的值:
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();
}
}
}
如果这是你的数据
“{”coord:{”lon:-0.13,“lat”:51.51},“天气”:[{”id:300,“main”:“毛毛雨”,“描述”:“光强度毛毛雨”,“图标”:“09d”},“基地”:“车站”,“main:{”temp:“温度”:280.32,“压力”:1012,“湿度”:81,“最低温度”:279.15,“最高温度”:281.15},“能见度”:10000,“风”:“{”速度:4.1,“度”:80},“云”:“{”all:“所有”:90},“dt 5789600,“系统类型”:id:{“:5091,“消息”:0.0103,“国家”:“GB”,“日出”:1485762037,“日落”:1485794875},“id”:2643743,“名称”:“伦敦”,“cod”:200}”
您只需在代码中进行一些更改即可检索
String json = "{\"temp\":296.88,\"feels_like\":298.86,\"temp_min\":296.88,\"temp_max\":296.88,\"pressure\":1013,\"humidity\":89,\"sea_level\":1013,\"grnd_level\":986}";
JSONObject weatherInfo = new JSONObject(json);
double temp = weatherInfo.getDouble("temp");
double feels_like = weatherInfo.getDouble("feels_like");
double temp_min = weatherInfo.getDouble("temp_min");
double temp_max = weatherInfo.getDouble("temp_max");
double pressure = weatherInfo.getDouble("pressure");
double humidity = weatherInfo.getDouble("humidity");
double sea_level = weatherInfo.getDouble("sea_level");
double grnd_level = weatherInfo.getDouble("grnd_level");
System.out.println(temp);
System.out.println(feels_like);
System.out.println(temp_min);
System.out.println(temp_max);
System.out.println(pressure);
System.out.println(humidity);
System.out.println(sea_level);
System.out.println(grnd_level);
String jsonString=“您的字符串”;
JSONObject json=新的JSONObject(jsonString);
JSONArray JSONArray=json.getJSONArray(“天气”);
for(int i=0;i
@shivas检索以下json{“main”:“Drizzle”,“description”:“light intensity Drizzle”,“icon”:“09d”},“base”:“stations”,“main”:{“temp”:280.32,“pressure”:1012,“湿度”:81,“temp_min”:279.15,“temp_max”:281.15},“能见度”:10000,“风”:{“速度”:4.1,“度”:80}
在源json中,请首先检查哪个属性是JSONArray和JSONObject
下面是检索它的代码
main
,wind
是JSONObject,而weather
是JSONArray
假设sourcejson是整个JSONObject
String jsonString = "your string";
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("weather");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject weatherObj = jsonArray.getJSONObject(i);
System.out.println(weatherObj);
}
String baseValue = json.getString("base");
Object mainValue = json.get("main");
Object visibilityValue = json.get("visibility");
Object windValue = json.get("wind");
JSONObject main=sourcejson.optJSONObject(“main”);
System.out.println(main.toString());
JSONObject wind=sourcejson.optJSONObject(“wind”);
System.out.println(wind.toString());
JSONArray weather=sourcejson.optJSONArray(“天气”);
对于(int i=0;i
试试这个,你会得到你的数据。它不是一个数组。你在JSONObject上使用
getString
,这会给你一个原子字符串。然后你将if传递给JSONArray的构造函数,它希望json数组是字符串,而不是字符串。谢谢你的帮助。我是个新来堆栈溢出的人。你的json与代码中的属性不匹配。确保发布了正确的JSON,以及是否要从jsonarray读取数据?
JSONObject main = sourcejson.optJSONObject("main");
System.out.println(main.toString());
JSONObject wind = sourcejson.optJSONObject("wind");
System.out.println(wind.toString());
JSONArray weather = sourcejson.optJSONArray("weather");
for (int i = 0; i < weather.length(); i++) {
JSONObject weatherObj = weather.getJSONObject(i);
System.out.println(weatherObj.toString());
}
String base = sourcejson.optString("base");
int visibility = sourcejson.optInt("visibility");