相关文章推荐
淡定的盒饭  ·  ASP.NET 核心 Blazor ...·  1 月前    · 
傻傻的馒头  ·  STRING_SPLIT ...·  1 月前    · 
犯傻的黄豆  ·  Branches API | GitLab ...·  1 周前    · 
无邪的柑橘  ·  MATLAB | ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

This is what i have tried

try {
        obj = new JSONParser().parse(new FileReader("jsonExampl.json"));
        JSONObject jo = (JSONObject) obj;
        empid = (String) jo.get("EmployeeId");
        firstname = (String) jo.get("FirstName");
        lastname = (String) jo.get("LastName");
        DOB = (String) jo.get("DOB");
        String ae = (String) jo.get("Age");
        System.out.println("Employee Id:- "+ empid);
        System.out.println("First Name:- "+ firstname);
        System.out.println("Last Name:- "+ lastname);
        System.out.println("DOB:- "+ DOB);
        System.out.println("Age:- "+ ae);
        Map address = ((Map)jo.get("Address"));
        Iterator<Map.Entry> itr1 = address.entrySet().iterator(); 
        while (itr1.hasNext()) { 
            Map.Entry pair = itr1.next(); 
            System.out.println(pair.getKey() + " : " + pair.getValue()); 
        phnumber=(String) jo.get("PhoneNumber");
        personalemail = (String) jo.get("PersonalMail");
        officialemail = (String) jo.get("OfficialMail");
        department = (String) jo.get("Department");
        DOJ = (String) jo.get("DOJ");
        Education = (String) jo.get("Education");
        System.out.println("Employee Phone:- "+ phnumber);
        System.out.println("Personal:- "+ personalemail);
        System.out.println("Official:- "+ officialemail);
        System.out.println("DOJ:- "+ DOJ);
        System.out.println("Education:- "+ Education);
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e.getMessage());

The code is not working when I try to parse the json array, so I get:

class org.json.simple.JSONArray cannot be cast to class org.json.simple.JSONObject (org.json.simple.JSONArray and org.json.simple.JSONObject are in unnamed module of loader 'app')

Please help Thank you in Advance

you are trying to parse it to a JSONObject, while you should be parsing it to a JSONArray, the error message is pretty clear – Stultuske Oct 20, 2022 at 6:47 Your top-level element is an array. array != object. array = list of things. object = thing with properties – knittl Oct 20, 2022 at 6:51

You have to parse it to JSONArray as in the comment above. This is the fixed code:

        JSONArray array = (JSONArray) new JSONParser().parse(json);
        for(Object obj : array.stream().toArray()) {
            JSONObject jo = (JSONObject) obj;
            String empid = (String) jo.get("EmployeeId");
            String firstname = (String) jo.get("FirstName");
            String lastname = (String) jo.get("LastName");
            String DOB = (String) jo.get("DOB");
            String ae = (String) jo.get("Age");
            System.out.println("Employee Id:- " + empid);
            System.out.println("First Name:- " + firstname);
            System.out.println("Last Name:- " + lastname);
            System.out.println("DOB:- " + DOB);
            System.out.println("Age:- " + ae);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.