英文:
Mapping nested JSON values in a Java class
问题
@Data
public class EventList {
@JsonProperty("_embedded")
private Events events;
}
@Data
public class Events {
@JsonProperty("events")
public List<EventDetails> eventsList;
}
@Data
public class EventDetails {
private String name;
@JsonProperty("dates.start.dateTime")
private String startDate;
@JsonProperty("classifications.genre.name")
private String musicType;
@JsonProperty("_embedded.venues")
private List<Venues> eventPlaceName;
}
@Data
public class Venues {
private String name;
}
英文:
I can't figure out how to handle nested JSON values in my Java classes. To make it simple as possible, I created four Java classes with every 'nested level'. However, I'm trying to have all these values in one Java class. How can I do this?
Json:
{
"_embedded":{
"events":[
{
"name":"KISS | End Of The Road World Tour",
"dates":{
"start":{
"dateTime":"2021-06-12T19:00:00Z"
},
"classifications":[
{
"name":"Rock"
}
],
"_embedded":{
"venues":[
{
"name":"Atlas Arena"
}
]
}
}
}
]
}
}
Java classes:
@Data
public class EventList {
@JsonProperty("_embedded")
private Events events;
}
@Data
public class Events {
@JsonProperty("events")
public List<EventDetails> eventsList;
}
@Data
public class EventDetails {
private String name;
@JsonProperty("dates.start.dateTime")
private String startDate;
@JsonProperty("classifications.genre.name")
private String musicType;
@JsonProperty("_embedded.venues")
private List<Venues> eventPlaceName;
}
@Data
public class Venues {
private String name;
}
答案1
得分: 1
你可以将所有的支持类都合并到一个类中,如下所示:
@Data
public class EventList {
@Getter
@JsonProperty("embedded")
private Events events;
@Data
public static class Venue {
@JsonProperty("name")
public String name;
}
@Data
public static class Classification {
@JsonProperty("name")
public String name;
}
@Data
public static class Embedded2 {
@JsonProperty("venues")
public List<Venue> venues;
}
@Data
public static class Dates {
@JsonProperty("start")
public Start start;
@JsonProperty("classifications")
public List<Classification> classifications;
@JsonProperty("embedded")
public Embedded2 embedded;
}
@Data
public static class EventDetails {
@JsonProperty("name")
public String name;
@JsonProperty("dates")
public Dates dates;
}
@Data
public static class Events {
@JsonProperty("events")
public List<EventDetails> eventsList;
}
@Data
public static class Start {
@JsonProperty("dateTime")
public Date dateTime;
}
}
在调试过程中,您会注意到我们的对象已经按照您的要求填充:
public class TestJson {
public static void main(String[] args) {
List<EventList.EventDetails> anyCity = findEventByCity("any city");
anyCity.stream().forEach(p -> {
System.out.println(p);
});
}
@SneakyThrows
static List<EventList.EventDetails> findEventByCity(String city) {
ObjectMapper objectMapper = new ObjectMapper();
EventList eventList = objectMapper.readValue(new File("C:\\Anurag\\Development\\CodeBase\\demo\\src\\main\\java\\com\\example\\demo\\domain\\testJson\\test.json"), EventList.class);
List<EventList.EventDetails> eventsList = eventList.getEvents().getEventsList();
return eventsList;
}
}
调试时,您会注意到我们的对象已经按照您的要求填充:
英文:
You can club all the supporting classes in one class like below :
@Data
public class Start {
@JsonProperty("dateTime")
public Date dateTime;
public static class Venue {
@JsonProperty("name")
public String name;
}
@Data
public static class Classification {
@JsonProperty("name")
public String name;
}
@Data
public static class Embedded2 {
@JsonProperty("venues")
public List<Venue> venues;
}
@Data
public static class Dates {
@JsonProperty("start")
public Start start;
@JsonProperty("classifications")
public List<Classification> classifications;
@JsonProperty("_embedded")
public Embedded2 _embedded;
}
@Data
public static class Event {
@JsonProperty("name")
public String name;
@JsonProperty("dates")
public Dates dates;
}
@Data
public static class Embedded {
@JsonProperty("events")
public List<Event> events;
}
@Data
public static class Root {
@JsonProperty("_embedded")
public Embedded _embedded;
}
}
You can test (I am using Jackson for deserialization)<br>
create ObjectMapper class and deserialize into a Root class
public class TestJson {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
try {
Start.Root root = objectMapper.readValue(new File("C:\\Anurag\\Development\\CodeBase\\demo\\src\\main\\java\\com\\example\\demo\\domain\\testJson\\test.json"), Start.Root.class);
System.out.println(root);
} catch (Exception e) {
e.printStackTrace();
}
}
}
-When debugging, you'll notice that our objects have been filled accordingly:
**Changes done as per your requiement **
@Data
public class EventList {
@Getter
@JsonProperty("_embedded")
private Events events;
@Data
public static class Venue {
@JsonProperty("name")
public String name;
}
@Data
public static class Classification {
@JsonProperty("name")
public String name;
}
@Data
public static class Embedded2 {
@JsonProperty("venues")
public List<Venue> venues;
}
@Data
public static class Dates {
@JsonProperty("start")
public Start start;
@JsonProperty("classifications")
public List<Classification> classifications;
@JsonProperty("_embedded")
public Embedded2 _embedded;
}
@Data
public static class EventDetails {
@JsonProperty("name")
public String name;
@JsonProperty("dates")
public Dates dates;
}
@Data
public static class Events {
@JsonProperty("events")
public List<EventDetails> eventsList;
}
@Data
public static class Start {
@JsonProperty("dateTime")
public Date dateTime;
}
}
public class TestJson {
public static void main(String[] args) {
List<EventList.EventDetails> anyCity = findEventByCity("any city");
anyCity.stream().forEach(p-> {
System.out.println(p);
});
}
@SneakyThrows
static List<EventList.EventDetails> findEventByCity(String city) {
ObjectMapper objectMapper = new ObjectMapper();
EventList eventList = objectMapper.readValue(new File("C:\\Anurag\\Development\\CodeBase\\demo\\src\\main\\java\\com\\example\\demo\\domain\\testJson\\test.json"), EventList.class);
List<EventList.EventDetails> eventsList = eventList.getEvents().getEventsList();
return eventsList;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论