-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

devmaria
  • 27
  • 7

3 Answers3

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;

    }
}
Smile
  • 3,832
  • 3
  • 25
  • 39
Arthurofos
  • 198
  • 1
  • 12
  • 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; – Arthurofos Feb 18 '20 at 09:31
  • It works wonderfull, thanks a million! I have last one question and edited the question up there. Would you look at that? – devmaria Feb 19 '20 at 12:05
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.

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);
TigerT
  • 52
  • 3
  • 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. – devmaria Feb 18 '20 at 08:51