如何将包含自定义对象列表的映射字段的自定义对象转换为 json?
How to convert custom object with map field that includes list of custom object to json?
比方说,我有一个员工 class,看起来像这样:
public class Employee{
Map<String, ArrayList<Salary>> salary = new HashMap<String, ArrayList<Salary>>();
String name;
String age;
}
public class Salary{
String amount;
String currency;
}
Javato/fromJson最聪明的转换方式是什么?
或者;
如果我的 json 看起来像这样怎么办:
{
"name": "Test",
"age": "12",
"salary": {
"first": {
"41130": {
"amount": "100",
"currency": "€"
},
"41132": {
"amount": "100",
"currency": "€"
}
},
"second": {
"41129": {
"amount": "100",
"currency": "€"
}
}
}
}
当我尝试将其转换为 Employee 时出现以下错误。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_ARRAY but was BEGIN_OBJECT
最聪明的方法,如果您打算在 Web 环境中使用 JSON 是使用 Jackson、JAXB 或您的基础结构已经提供的任何东西,例如让您首选的 REST 基础架构为您完成这项工作。
您需要提供更多关于应用程序用途或所需架构的上下文。
我想你可以使用 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);
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;
}
}
比方说,我有一个员工 class,看起来像这样:
public class Employee{
Map<String, ArrayList<Salary>> salary = new HashMap<String, ArrayList<Salary>>();
String name;
String age;
}
public class Salary{
String amount;
String currency;
}
Javato/fromJson最聪明的转换方式是什么?
或者;
如果我的 json 看起来像这样怎么办:
{
"name": "Test",
"age": "12",
"salary": {
"first": {
"41130": {
"amount": "100",
"currency": "€"
},
"41132": {
"amount": "100",
"currency": "€"
}
},
"second": {
"41129": {
"amount": "100",
"currency": "€"
}
}
}
}
当我尝试将其转换为 Employee 时出现以下错误。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
最聪明的方法,如果您打算在 Web 环境中使用 JSON 是使用 Jackson、JAXB 或您的基础结构已经提供的任何东西,例如让您首选的 REST 基础架构为您完成这项工作。
您需要提供更多关于应用程序用途或所需架构的上下文。
我想你可以使用 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);
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;
}
}