0

I have the following Json and I want to parse the array (cars) ,

[
{
"name": "John",
"city": "Berlin",
"cars": [
"audi",
"bmw"
],

when i tried with the following code i got error

JSONParser parser = new JSONParser();

    JSONArray a = (JSONArray) parser.parse(new FileReader(
            "C:\\General\\Json\\json.txt"));

    for (Object o : a) {
        JSONObject person = (JSONObject) o;

        String name = (String) person.get("name");
        System.out.println(name);

        String city = (String) person.get("city");
        System.out.println(city);

        String job = (String) person.get("job");
        System.out.println(job);

    }

here is the error "jsonObject cannot be resolved" how should i overcome it?

JSONArray cars = (JSONArray) jsonObject.get("cars");

2 Answers 2

1
 you did not declared jsonObject 
Sign up to request clarification or add additional context in comments.

Comments

0

JSONArray cars = (JSONArray) person.get("cars"); try this instead of JSONArray cars = (JSONArray) jsonObject.get("cars"); this PSR also correct

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.