0

I have written a program that reads a simple json file:

public static void main(String[] args) {
    JSONParser parser = new JSONParser();
    try {
        JSONArray a = (JSONArray) parser.parse(new FileReader("C:/Users/Zonoid/Desktop/EQ.json"));
        for (Object o : a)
        {
            JSONObject obj = (JSONObject) o;
            String city = (String) obj.get("CITY");
            System.out.println("City : " + city);
            String loc = (String) obj.get("LOCATION");
            System.out.println("Location : " + loc);
            long el = (Long) obj.get("E_LEVEL");
            System.out.println("Emergency Level : " + el);
            long depth = (Long) obj.get("DEPTH");
            System.out.println("Depth : " + depth);
            long i = (Long) obj.get("INTENSITY");
            System.out.println("Intensity :"+i);
            System.out.println("\n");
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

with my json file being:

[{"CITY":"MUMBAI","LOCATION":"a" ,"E_LEVEL": 6,"DEPTH":10,"INTENSITY":5},
{"CITY":"MUMBAI","LOCATION":"b" ,"E_LEVEL": 8,"DEPTH":20,"INTENSITY":4},
{"CITY":"MUMBAI","LOCATION":"c" ,"E_LEVEL": 3,"DEPTH":13,"INTENSITY":5},
{"CITY":"MUMBAI","LOCATION":"d" ,"E_LEVEL": 6,"DEPTH":12,"INTENSITY":4},]

I am working on a project that deals with earthquake alerts and want to read their JSON files however I cannot import them in JSON Array. The file I want to import looks like this:

{
  "type": "FeatureCollection",
  "metadata": {
    "generated": 1488472809000,
    "url": "https:\/\/earthquake.usgs.gov\/earthquakes\/feed\/v1.0\/summary\/significant_week.geojson",
    "title": "USGS Significant Earthquakes, Past Week",
    "status": 200,
    "api": "1.5.4",
    "count": 2
  },
  "features": [
    {
      "type": "Feature",
      "properties": {
        "mag": 5.5,
        "place": "42km WSW of Anchor Point, Alaska",
        "time": 1488420690658,....

Please tell what changes should be made.

9
  • What is wrong with the code you posted? Commented May 26, 2017 at 7:02
  • it gives the error org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray at practice.json.main(json.java:17) Commented May 26, 2017 at 7:06
  • I see now. You need to change your json file. The json file contains a object (it starts with { and ends with }. You want it to be an object array (starts with [{ and ends with }]. Commented May 26, 2017 at 7:08
  • First if I am correct you are working on Udacity project app and from the site what you get is JSON object in that you have features array from where you should be extracting the objects not directly from the data for have in file Commented May 26, 2017 at 7:09
  • I tried that but it didn't remove the error Commented May 26, 2017 at 7:10

1 Answer 1

2

If you are trying to read from features only, first you need to read the whole file as an object. Then you can, read the array part in the following way:

Object object = parser.parse(new FileReader("C:/Users/Zonoid/Desktop/EQ.json"));
JSONObject jasonObject = (JSONObject) object;
JSONArray features = (JSONArray) jasonObject.get("features");
Sign up to request clarification or add additional context in comments.

1 Comment

ok, my mistake was directly using JSONObject with parser.parse without converting it. Thanks for the help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.