Home » Java » java – How to convert custom object with map field that includes list of custom object to json?-Exceptionshub

java – How to convert custom object with map field that includes list of custom object to json?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Let’s say, I have an Employee class which looks like this:

public class Employee{

Map<String, ArrayList<Salary>> salary = new HashMap<String, ArrayList<Salary>>();
String name;
String age;
}

public class Salary{
String amount;
String currency;
}

What is the smartest way of convertion to/from Json in Java?

Or;

What if my json should look like that:

  {
  "name": "Test",
  "age": "12",
  "salary": {
    "first": {
      "41130": {
        "amount": "100",
        "currency": "€"
      },
      "41132": {
        "amount": "100",
        "currency": "€"
      }
    },
    "second": {
      "41129": {
        "amount": "100",
        "currency": "€"
      }
    }
  }
}

When i tried to convert this to Employee I am getting error below.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was BEGIN_OBJECT

How to&Answers:
public class Main {

    public static void main(String[] args) {

        Gson gson = new Gson();

        Map<String, ArrayList<Salary>> sal = new HashMap<String, ArrayList<Salary>>();
        ArrayList<Salary> salaries = new ArrayList<Salary>();
        Salary salary1 = new Salary("100", "€");
        Salary salary2 = new Salary("200", "€");
        salaries.add(salary1);
        salaries.add(salary2);
        sal.put("1", salaries);
        Employee employee = new Employee(sal, "Test", "12");

        System.out.println("Age -> " + employee.getAge());
        System.out.println("Name -> " + employee.getName());
        System.out.println("Salary -> " + employee.getSalary());

        String json = gson.toJson(employee);
        System.out.println("Json -> " + json);

        Employee employee1 = gson.fromJson(json, Employee.class);

        System.out.println("Age1 -> " + employee1.getAge());
        System.out.println("Name1 -> " + employee1.getName());
        System.out.println("Salary1 -> " + employee1.getSalary());
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Employee{

        Map<String, ArrayList<Salary>> salary = new HashMap<String, ArrayList<Salary>>();
        String name;
        String age;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Salary{
        String amount;
        String currency;

    }
}

Answer:

Smartest way, if you plan to use that JSON on a web environment is to use Jackson, JAXB or anything your infraestructure already provides, e.g. Let your preferred REST infrastructure do the job for you.

You will need to provide more context on what is the purpose of your application or the required architecture.

Answer:

I think you can use Jackson ObjectMapper
https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html#writeValue(java.io.OutputStream,%20java.lang.Object)

ObjectMapper objectMapper = new ObjectMapper();
Employee employee = new Employee();
objectMapper.writeValue(new FileOutputStream("data/output.json"), employee);