Возникли проблемы при разборе JSON данных с использованием Java - PullRequest
0 голосов
/ 31 марта 2020

Я пытаюсь проанализировать json данные в Netbeans, но получаю ошибку. Ошибка:

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 голос
/ 31 марта 2020

Спасибо, ПМ 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; 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...