1

I have

data= [{
          id=1, 
          employee_name=Tiger Nixon, 
          employee_salary=320800, 
          employee_age=61, 
          profile_image=
       }, 
       {
          id=2, 
          employee_name=Garrett Winters, 
          employee_salary=170750, 
          employee_age=63, 
          profile_image=
       }, 
       {
          id=3, 
          employee_name=Ashton Cox, 
          employee_salary=86000, 
          employee_age=66, 
          profile_image=
       }, 
       {
          id=4, 
          employee_name=Cedric Kelly, 
          employee_salary=433060, 
          employee_age=22, 
          profile_image=
       }
      ]

I have employee class

public class Employee {
    private String employee_name;
    private String employee_salary;
    private String employee_age;
    private String id;
    private String profile_image;

    public String toCsvRow() {
        String csvRow = "";
        for (String value : Arrays.asList(employee_name,employee_salary,employee_age)) {
            String processed = value;
            if (value.contains("\"") || value.contains(",")) {
                processed = "\"" + value.replaceAll("\"", "\"\"") + "\"";
            }
            csvRow += "," + processed;
        }
        return csvRow.substring(1);
    }

    public String getEmployee_name() {
        return employee_name;
    }

    public String getEmployee_salary() {
        return employee_salary;
    }

    public String getEmployee_age() {
        return employee_age;
    }          
}

I tried for

Map<String, ArrayList<Employee>> map = mapper.readValue(url, Map.class);
ArrayList<Employee> emps = map.get("data");
emps.get(0).toCsvRow()

Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.example.model.Employee

Now I cannot call toCSVRow using emps.

6
  • It’s not clear what your asking and what your issue is. Is there a problem with map or with converting the content of it to a list of Employee objects? Commented Jan 24, 2020 at 6:49
  • First thing I don't understand is this Arrays.asList(employee_name,employee_salary,employee_age), shouldn't it be expecting an array? Second is do you want to create a excel file or store it to a variable named csvRow Commented Jan 24, 2020 at 6:54
  • yes I want it to store in csv format Commented Jan 24, 2020 at 6:57
  • update the question I want csv from this employee data Commented Jan 24, 2020 at 6:57
  • I think it should be Map<String, ArrayList<Employee>> map = mapper.readValue(data, new TypeReference<Map<String,ArrayList<Employee>>>(){});rather than Map<String, ArrayList<Employee>> map = mapper.readValue(url, Map.class);. Read Map from JSON String reference : tutorials.jenkov.com/java-json/jackson-objectmapper.html Commented Jan 24, 2020 at 7:18

1 Answer 1

4

Use Gson to parse JSON to ArrayList, for CSV conversion you can use org.json.CDL

See this is working fine

String str = "[{" +
            "          id=1," +
            "          employee_name=\"Tiger Nixon\"," +
            "          employee_salary=320800," +
            "          employee_age=61," +
            "          profile_image=\"khkjh\"" +
            "       }," +
            "       {" +
            "          id=2," +
            "          employee_name=\"Garrett Winters\"," +
            "          employee_salary=170750," +
            "          employee_age=63," +
            "          profile_image=\"\"" +
            "       }," +
            "       {" +
            "          id=3," +
            "          employee_name=\"Ashton Cox\"," +
            "          employee_salary=86000," +
            "          employee_age=66," +
            "          profile_image=\"\"" +
            "       }," +
            "       {" +
            "          id=4," +
            "          employee_name=\"Cedric Kelly\"," +
            "          employee_salary=433060," +
            "          employee_age=22," +
            "          profile_image=\"\"" +
            "       }" +
            "      ]";


         try{

            Gson gson = new Gson();
            ArrayList<Employee> list = gson.fromJson(str, ArrayList.class);

            String csv = CDL.toString(new JSONArray(list));

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

Output:

id,employee_name,employee_salary,employee_age,profile_image
    1.0,Tiger Nixon,320800.0,61.0,khkjh
    2.0,Garrett Winters,170750.0,63.0,
    3.0,Ashton Cox,86000.0,66.0,
    4.0,Cedric Kelly,433060.0,22.0
Sign up to request clarification or add additional context in comments.

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.