How to parse a json file into a json object in java

Vasko Vasilev :

I am trying to parse a json file in java. However I keep receiving an error. This is the file I am trying to parse:

[{
        "name": "John Smith",
        "totalSales": 250,
        "salesPeriod": 10,
        "experienceMultiplier": 0.5
    },
    {
        "name": "David Prowless",
        "totalSales": 250,
        "salesPeriod": 10,
        "experienceMultiplier": 0.5
    }
]

This is what I have tried:

public static void main(String[] args)
{
    JSONParser parser = new JSONParser();

    try {     
        Object obj = parser.parse(new FileReader("data.json"));

        JSONObject jsonObject =  (JSONObject) obj;

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

        String totalSales = (String) jsonObject.get("totalSales");
        System.out.println(totalSales);

        String salesPeriod = (String) jsonObject.get("salesPeriod");
        System.out.println(salesPeriod);

        String exp = (String) jsonObject.get("exp");
        System.out.println(exp);


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

This is the error I receive:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
at mentormate.json.MentormateJson.main(MentormateJson.java:23)
Java Result: 1

I apologize if this is a silly question with a simple solution. I am new to json.

EDIT:

I have decided to go along with the code below. However, I cannot set the for each loop right to iterate through object in the json file.

 public static void main(String[] args) {

 JSONParser parser = new JSONParser();

    try {
        Object obj = parser.parse(new FileReader("data.json"));

        JSONArray jsonObjects =  (JSONArray) obj;

        for ( JSONObject jsonObject : jsonObjects) {
            String name = (String) jsonObject.get("name");
            System.out.println(name);

            String totalSales = (String) jsonObject.get("totalSales");
            System.out.println(totalSales);

            String salesPeriod = (String) jsonObject.get("salesPeriod");
            System.out.println(salesPeriod);

            String exp = (String) jsonObject.get("exp");
            System.out.println(exp);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

FINAL EDIT (PROBLEM SOLVED):

public static void main(String[] args)   {
    JSONParser parser = new JSONParser();

    try {
        Object obj = parser.parse(new FileReader("data.json"));

        JSONArray jsonObjects =  (JSONArray) obj;

        for (Object o : jsonObjects) {
            JSONObject jsonObject = (JSONObject) o;
            String name = (String) jsonObject.get("name");
            System.out.println(name);

            Long totalSales = (Long) jsonObject.get("totalSales");
            System.out.println(totalSales);

            Long salesPeriod = (Long) jsonObject.get("salesPeriod");
            System.out.println(salesPeriod);

            Double exp = (Double) jsonObject.get("experienceMultiplier");
            System.out.println(exp);

            System.out.println();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Sergey Bzhezitskiy :

Please try this:

 public static void main(String[] args) {

    JSONParser parser = new JSONParser();

    try {
        Object obj = parser.parse(new FileReader("data.json"));

        JSONArray jsonObjects =  (JSONArray) obj;

        for (Object o : jsonObjects) {
            JSONObject jsonObject = (JSONObject) o;
            String name = (String) jsonObject.get("name");
            System.out.println(name);

            Long totalSales = (Long)jsonObject.get("totalSales");
            System.out.println(totalSales);

            String salesPeriod = (String) jsonObject.get("salesPeriod");
            System.out.println(salesPeriod);

            String exp = (String) jsonObject.get("exp");
            System.out.println(exp);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related