У меня есть класс
public class Car {
public String color = null;
public String brand = null;
public HashMap<String,String> attributes;
public Car() {
this.attributes = new HashMap<>();
}
public static void main( String[] args ) {
Car car = new Car();
car.color = "Red";
car.brand = "Hyundai";
car.attributes.put("key1", "value1");
car.attributes.put("key2", "value1");
Gson gson = new Gson();
String json = gson.toJson(car);
System.out.println(json);
}
}
В настоящее время он сериализует мой car
объект в
{"color":"Red","brand":"Hyundai","attributes":{"key1":"value1","key2":"value1"}}
Но я хотел бы распаковать и сериализовать словарь attributes
из Car
класс как отдельные свойства, а не словарь. В идеале я хотел бы, чтобы мой json был
{"color":"Red","brand":"Hyundai","key1":"value1","key2":"value1"}
Как мне добиться того же в GSON?