-2

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

4
  • Does it answer your question: stackoverflow.com/questions/15786129/… Commented Feb 18, 2020 at 6:34
  • Unfortunately doesn't. Maps, especially Map with an ArrayList in it more complex than just a List. Commented Feb 18, 2020 at 7:04
  • instaed of Map<String, ArrayList<Salary>> salary = new HashMap<String, ArrayList<Salary>>(); its better to implement on the in List interface: Map<String, List<Salary>> salary = new HashMap<String, ArrayList<Salary>>(); Commented Feb 18, 2020 at 9:33
  • What differences that can make? @Arthurofos Commented Feb 19, 2020 at 19:55

3 Answers 3

1
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;

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

import com.google.gson.Gson; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.HashMap; import java.util.Map;
It works wonderfull, thanks a million! I have last one question and edited the question up there. Would you look at that?
0

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.

Comments

0

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);

1 Comment

This is not what i'm looking for. Returns wrong answer. But mapper.readTree() returned correct. However now I need to create Employee object from this JsonNode.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.