1

I have a json file like this

[ {
    "id":"serve-coffee",
    "tags":[ {
        "name": "@tag1", "line": 1
    }
    ],
    "description":"Coffee should not be served\n",
    "name":"Serve coffee",
    "keyword":"Feature",
    "line":2,
    "elements":[ {
        "id": "serve-coffee;buy-last-coffee", "tags":[ {
            "name": "@tag2", "line": 6
        }
        ],
        "description":"",
        "name":"Buy last coffee",
        "keyword":"Scenario",
        "line":7,
        "steps":[ {
            "name": "there are 1 coffees left in the machine", "keyword": "Given ", "line": 8
        }
        ,
        {
            "name": "I have deposited 1$", "keyword": "And ", "line": 9
        }
        ],
        "type":"scenario"
    }
    ],
    "uri":"src\/test\/resources\/traffic-remove-locations.feature"
}

]

Iam trying to convert the above json file to JSONObject.But am getting class cast exception "java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject"

code

public static JSONObject convertFileToJSON(String fileName) throws ParseException {

        // Read from File to String
        JSONParser parser = new JSONParser();
        JSONObject jsonObject = null;
        try {
            Object object = parser.parse(new FileReader(fileName));         
            jsonObject = (JSONObject) object;   // Getting classCast Exception here.

        } catch (FileNotFoundException e) {

        } catch (IOException ioe) {

        }       
        return jsonObject;
    }

but when i changed the line jsonObject = (JSONObject) object; to JSONArray jsonArray = (JSONObject)object the exception disappears. But if am casting to JSONArray then how can i get the values like id,tags and description from JSONArray. please provide a suggestion guys

3 Answers 3

4

Your JSON file represents an array with one object in it. So if that were a Java data structure, you're effectively doing this:

int[] arr = {5};
int i = (int)arr;

This obviously doesn't work because you can't cast an array to a singular object. What you actually want to do it pull out the first element of the array. To continue the Java example, you want to do

int[] arr = {5};
int i = (int)arr[0];

With the JSON stuff, your parser.parse() call returns a JSONArray, not a JSONObject. So you'll need to do something like this:

public static JSONObject convertFileToJSON(String fileName) throws ParseException {
    JSONParser parser = new JSONParser();
    JSONObject jsonObject = null;
    try {
        JSONArray array = (JSONArray) parser.parse(new FileReader(fileName));         
        jsonObject = array.getJsonObject(0);

    } catch (FileNotFoundException e) {

    } catch (IOException ioe) {

    }       
    return jsonObject;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try casting to JsonArray and then cast access the objects one by one with help of the index from the JSON array.

 Object object = parser.parse(new FileReader(fileName));         
 JsonArray  jsonArr = (JsonArray) object;   // Getting c
 jsonObject jsonObj = jsonArr.get(0);  //use index to access like a list

Comments

0

I was also facing the same issue. I did the below changes to make it work. Below is my sample JSON. {

"routes": [{

        "startTime": 1520319414,
        "routeInfo": [{
                "routePart": 0,
                "transType": "WALK",
                "transDetails": {
                    "startLoc": {
                        "lat": 28.6434862,
                        "lon": 77.22542659999999
                    }
                }
            }, {
                "routePart": 1,
                "transType": "BUS",
                "transDetails": {
                    "routeNumber": "307-U",
                    "interStopDetails": [{
                            "seq": 1,
                            "name": "test1",
                            "loc": {
                                "lat": 28.64302,
                                "lon": 77.2260367
                            },
                            "eta": 1520319600
                        }
                    ]
                }
            }
        ],
        "totalTime": 5742
    }
]

}

Solution to Parse: JSONObject obj = (JSONObject) jsonParser.parse(new FileReader(FilepathDummy));

        JSONArray jsonRoutes=  (JSONArray) obj.get("routes"); //Gives Main JSoN

        JSONObject  routeJsonObject = (JSONObject) jsonRoutes.get(0); // Route Array into JSON

        JSONArray routeInfoArray = (JSONArray) routeJsonObject.get("routeInfo"); //  RouteInfo Array

Hope this solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.