Json对象未在Java中显示

Json对象未在Java中显示,java,json,Java,Json,我想从JSONObject获取“天气”数据,但这个错误即将发生 org.json.JSONException: JSONObject["weather"] not a string. at org.json.JSONObject.getString(JSONObject.java:639) at GetWeather.main(GetWeather.java:49) 这是我的密码 import java.io.IOException; import java.io.InputS

我想从JSONObject获取“天气”数据,但这个错误即将发生

org.json.JSONException: JSONObject["weather"] not a string.
    at org.json.JSONObject.getString(JSONObject.java:639)
    at GetWeather.main(GetWeather.java:49)
这是我的密码

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class GetWeather {       

    public static String getWeather(String args){
        String result =" ";
        URL url ;
        HttpURLConnection urlConnection = null;
        try{
            url = new URL(args);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data= reader.read();
            while(data!=-1){
                char current=(char) data;
                result += current;
                data= reader.read();
            }
            return result;
        }catch(MalformedURLException e){
             e.printStackTrace();
        } catch (IOException e) { 
            e.printStackTrace();
        }
        return null;
    }

    //main
    public static void main(String[] args){
        String s1 = getWeather(args[0]);
        try {
            JSONObject jsonObject = new JSONObject(s1);
            String weather= jsonObject.getString("weather");
            System.out.println(weather);
        } catch (JSONException e) { 
            e.printStackTrace();
        }
    }
}
这是我正在传递的绳子

http://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=04b767167643ea6af521695e7948e0fb
这是我得到的数据

{"coord":{"lon":77.22,"lat":28.67},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50d"}],"base":"stations","main":{"temp":305.86,"pressure":1007,"humidity":38,"temp_min":304.15,"temp_max":307.15},"visibility":3500,"wind":{"speed":1.5,"deg":320},"clouds":{"all":0},"dt":1508241600,"sys":{"type":1,"id":7808,"message":0.0051,"country":"IN","sunrise":1508201604,"sunset":1508242734},"id":1273294,"name":"Delhi","cod":200}
在格式化的版本中

{
    "coord": {
        "lon": 77.22,
        "lat": 28.67
    },
    "weather": [{
            "id": 721,
            "main": "Haze",
            "description": "haze",
            "icon": "50d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 305.86,
        "pressure": 1007,
        "humidity": 38,
        "temp_min": 304.15,
        "temp_max": 307.15
    },
    "visibility": 3500,
    "wind": {
        "speed": 1.5,
        "deg": 320
    },
    "clouds": {
        "all": 0
    },
    "dt": 1508241600,
    "sys": {
        "type": 1,
        "id": 7808,
        "message": 0.0051,
        "country": "IN",
        "sunrise": 1508201604,
        "sunset": 1508242734
    },
    "id": 1273294,
    "name": "Delhi",
    "cod": 200
}
请告诉我我的代码有什么问题,该怎么办。

您试图获取的“weather”值不是
字符串,而是
JSONArray

要读取其中的所有信息,请尝试使用
getJSONArray()

对于其他信息,如“temp”或“pressure”,只需使用
getJSONObject()
,因为“main”具有
JSONObject
类型:

 JSONObject mainObject = jsonObject.getJSONObject("main");
 System.out.println("pressure value: " + mainObject.get("pressure"));
 System.out.println("temp value: " + mainObject.get("temp"));

“weather”键不保存字符串值,但数组
[…]
因此
getString(“weather”)
不是选择数组的正确方法。你试过类似于
getJSONArray
的东西吗?所以你不应该使用
getString()
谢谢,@Pshemo它起作用了。你能告诉我从中提取温度和压力的方法吗?谢谢,@Neo它起作用了,你能告诉我从中提取温度和压力的方法吗?我将你的问题格式化,以添加更具可读性的JSON版本。我还删除了ASAP,因为它在问题中不是真正必要的(并且是非常好的向下投票磁铁,原因解释如下:)
 JSONObject mainObject = jsonObject.getJSONObject("main");
 System.out.println("pressure value: " + mainObject.get("pressure"));
 System.out.println("temp value: " + mainObject.get("temp"));