java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject error when reading json file iwith java

mustafa53 :

I have a json file which I'm creating it some vba codes in excel. I want to read this json file with java. To do this I wrote this codes

try {
        Class.forName("org.json.simple.parser.JSONParser");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    JSONParser parser = new JSONParser();

    try {
        System.out.println("Reading JSON file from Java program");
        FileReader fileReader = new FileReader("C:\\Users\\ftk1187\\Desktop\\jsonExample.json");
        JSONArray jsonArray = (JSONArray) parser.parse(fileReader);
        JSONObject json=(JSONObject) jsonArray.get(0);

        String reference = (String) json.get("reference");
        String refType = (String) json.get("refType");
        String engType = (String) json.get("engType");

        System.out.println("reference: " + reference);
        System.out.println("refType: " + refType);
        System.out.println("refType: " + refType);
        System.out.println("engType: " + engType);

        /*JSONArray characters = (JSONArray) json.get("Task");
        Iterator i =  characters.iterator();

        System.out.println("characters: ");
        while (i.hasNext()) {
            System.out.println(" " + i.next());
        }*/

    } catch (Exception ex) {
        ex.printStackTrace();
    }

But the program gives me this error java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

My json file like this.

[
   {
      "Task": {
         "refType": "EM TASK ",
         "reference": "72-00-32-020-004",
         "engType": "A5",
         "DMC": "V2500-A0-72-00-3200-04A-520A-B",
         "subTasks": {
            "SUBTASK0": "72-00-32-020-065",
            "SUBTASK1": "72-00-32-020-066",
            "SUBTASK2": "72-00-32-020-067",
            "SUBTASK3": "72-00-32-020-068",
            "SUBTASK4": "72-00-32-020-069",
            "SUBTASK5": "72-00-32-020-070-A00",
            "SUBTASK6": "72-00-32-020-070-B00",
            "SUBTASK7": "72-0-32-020-070-C00",
            "SUBTASK8": "72-00-32-020-070-D00",
            "SUBTASK9": "72-00-32-020-070-E00",
            "SUBTASK10": "72-00-32-020-070-F00",
            "SUBTASK11": "72-00-32-020-070-G00",
            "SUBTASK12": "72-00-32-020-071",
            "SUBTASK13": "72-00-32-020-072",
            "SUBTASK14": "72-00-32-020-058",
            "SUBTASK15": "72-00-32-020-073",
            "SUBTASK16": "72-00-32-020-074-A00",
            "SUBTASK17": "72-00-32-020-074-B00",
            "SUBTASK18": "72-00-32-020-075-A00",
            "SUBTASK19": "72-00-32-020-075-B00",
            "SUBTASK20": "72-00-32-020-323",
            "SUBTASK21": "72-00-32-020-324",
            "SUBTASK22": "72-00-32-020-076-A00",
            "SUBTASK23": "72-00-32-020-076-B00",
            "SUBTASK24": "72-00-32-020-077",
            "SUBTASK25": "72-00-32-020-078-A00",
            "SUBTASK26": "72-00-32-020-078-B00",
            "SUBTASK27": "72-00-32-020-079"
         }
      }
   },

Eclipse's console shows this

Reading JSON file from Java program
reference: null
refType: null
refType: null
engType: null
Iskuskov Alexander :

There is an json array at the top level of your document since:

A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values.

If your document has only one element, you can simply remove the square brackets. Otherwise just get JSONObject from JSONArray:

JSONArray jsonArray = (JSONArray) parser.parse(fileReader);
JSONObject json = (JSONObject) jsonArray.get(0);

JSONObject jsonValue = (JSONObject) json.get("Task");
String reference = (String) jsonValue.get("reference");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=359136&siteId=1