有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java解析JSON数据时遇到问题

我试图在Netbeans中解析json数据,但我得到了一个错误。错误是:

java.lang.ClassCastException: class org.json.simple.JSONObject cannot be cast to class 
org.json.simple.JSONArray (org.json.simple.JSONObject and org.json.simple.JSONArray are in 
unnamed module of loader 'app')

我的代码如下所示:

JSONParser parser = new JSONParser();
        try
        {
            Object object = parser.parse(new FileReader("currentWeather7.json"));
            JSONObject jsonObject = (JSONObject)object;
            JSONArray wez = (JSONArray)jsonObject.get("currently");
            for (Iterator it = wez.iterator(); it.hasNext();) {
                JSONObject current = (JSONObject) it.next();
                lineWeather = "In your area, it is currently: " + current.get("summary") + "\n" +
                 "It is " + current.get("temperature") + " degrees outside with a humidity of " + current.get("humidity") + "%."
                    + " The wind speed is " + current.get("windSpeed") + " miles per hour."; 

        }


    }
    catch(FileNotFoundException fe)
    {
        fe.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

     return lineWeather; 

它应该返回的文本是:

In your area, it is currently: Clear.
It is 51.66 degrees outside with a humidity of .44%. The wind speed is 9.22 miles per hour.

我试图解析的数据如下所示:

{
  "currently": {
    "summary": "Clear",
    "precipProbability": 0,
    "visibility": 10,
    "windGust": 9.22,
    "precipIntensity": 0,
    "icon": "clear-day",
    "cloudCover": 0.15,
    "windBearing": 26,
    "apparentTemperature": 49.51,
    "pressure": 1019.8,
    "dewPoint": 30.2,
    "ozone": 318.2,
    "nearestStormBearing": 32,
    "nearestStormDistance": 243,
    "temperature": 51.66,
    "humidity": 0.44,
    "time": 1585668360,
    "windSpeed": 9.22,
    "uvIndex": 2
  },
  "offset": -7,
  "timezone": "America/Phoenix",
  "latitude": 34.856894,
  "longitude": -111.788274
}

请让我知道我需要修理什么。谢谢大家!


共 (1) 个答案

  1. # 1 楼答案

    谢谢你,PM 77-1。我更改了代码,现在它可以工作了:

    JSONParser parser = new JSONParser();
        try
        {
            Object object = parser.parse(new FileReader("currentWeather7.json"));
    
            //new FileWriter("trailSorter7.txt", false).close();
            //convert Object to JSONObject
            JSONObject jsonObject = (JSONObject)object;
            JSONObject weath = (JSONObject) jsonObject.get("currently");
            //Object stez = weath.get("temperature");
            lineWeather = "In your area, it is currently: " + weath.get("summary").toString() + "\n" +
                        "It is " + weath.get("temperature").toString() + " degrees outside with a humidity of " + weath.get("humidity").toString() + "%."
                        + " The wind speed is " + weath.get("windSpeed").toString() + " miles per hour."; 
            System.out.println(lineWeather);
    
    
        }
        catch(FileNotFoundException fe)
        {
            fe.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    
         return lineWeather;