映射Java类中的嵌套JSON值

huangapple go评论72阅读模式
英文:

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:

{
   &quot;_embedded&quot;:{
      &quot;events&quot;:[
         {
            &quot;name&quot;:&quot;KISS | End Of The Road World Tour&quot;,
            &quot;dates&quot;:{
               &quot;start&quot;:{
                  &quot;dateTime&quot;:&quot;2021-06-12T19:00:00Z&quot;
               },
               &quot;classifications&quot;:[
                  {
                     &quot;name&quot;:&quot;Rock&quot;
                  }
               ],
               &quot;_embedded&quot;:{
                  &quot;venues&quot;:[
                     {
                        &quot;name&quot;:&quot;Atlas Arena&quot;
                     }
                  ]
               }
            }
         }
      ]
   }
}

Java classes:

@Data
public class EventList {

    @JsonProperty(&quot;_embedded&quot;)
    private Events events;
}


@Data
public class Events {

    @JsonProperty(&quot;events&quot;)
    public List&lt;EventDetails&gt; eventsList;
}

@Data
public class EventDetails {

    private String name;
    @JsonProperty(&quot;dates.start.dateTime&quot;)
    private String startDate;
    @JsonProperty(&quot;classifications.genre.name&quot;)
    private String musicType;
    @JsonProperty(&quot;_embedded.venues&quot;)
    private List&lt;Venues&gt; 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;
    }
}

调试时,您会注意到我们的对象已经按照您的要求填充:

映射Java类中的嵌套JSON值

英文:

You can club all the supporting classes in one class like below :

@Data
public class Start {
@JsonProperty(&quot;dateTime&quot;)
public Date dateTime;
public static class Venue {
@JsonProperty(&quot;name&quot;)
public String name;
}
@Data
public static class Classification {
@JsonProperty(&quot;name&quot;)
public String name;
}
@Data
public static class Embedded2 {
@JsonProperty(&quot;venues&quot;)
public List&lt;Venue&gt; venues;
}
@Data
public static class Dates {
@JsonProperty(&quot;start&quot;)
public Start start;
@JsonProperty(&quot;classifications&quot;)
public List&lt;Classification&gt; classifications;
@JsonProperty(&quot;_embedded&quot;)
public Embedded2 _embedded;
}
@Data
public static class Event {
@JsonProperty(&quot;name&quot;)
public String name;
@JsonProperty(&quot;dates&quot;)
public Dates dates;
}
@Data
public static class Embedded {
@JsonProperty(&quot;events&quot;)
public List&lt;Event&gt; events;
}
@Data
public static class Root {
@JsonProperty(&quot;_embedded&quot;)
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(&quot;C:\\Anurag\\Development\\CodeBase\\demo\\src\\main\\java\\com\\example\\demo\\domain\\testJson\\test.json&quot;), Start.Root.class);
System.out.println(root);
} catch (Exception e) {
e.printStackTrace();
}
}
}

-When debugging, you'll notice that our objects have been filled accordingly:
映射Java类中的嵌套JSON值

**Changes done as per your requiement **

@Data
public class EventList {
@Getter
@JsonProperty(&quot;_embedded&quot;)
private Events events;
@Data
public static class Venue {
@JsonProperty(&quot;name&quot;)
public String name;
}
@Data
public static class Classification {
@JsonProperty(&quot;name&quot;)
public String name;
}
@Data
public static class Embedded2 {
@JsonProperty(&quot;venues&quot;)
public List&lt;Venue&gt; venues;
}
@Data
public static class Dates {
@JsonProperty(&quot;start&quot;)
public Start start;
@JsonProperty(&quot;classifications&quot;)
public List&lt;Classification&gt; classifications;
@JsonProperty(&quot;_embedded&quot;)
public Embedded2 _embedded;
}
@Data
public static class EventDetails {
@JsonProperty(&quot;name&quot;)
public String name;
@JsonProperty(&quot;dates&quot;)
public Dates dates;
}
@Data
public static class Events {
@JsonProperty(&quot;events&quot;)
public List&lt;EventDetails&gt; eventsList;
}
@Data
public static class Start {
@JsonProperty(&quot;dateTime&quot;)
public Date dateTime;
}
}

public class TestJson {
public static void main(String[] args) {
List&lt;EventList.EventDetails&gt; anyCity = findEventByCity(&quot;any city&quot;);
anyCity.stream().forEach(p-&gt; {
System.out.println(p);
});
}
@SneakyThrows
static List&lt;EventList.EventDetails&gt; findEventByCity(String city) {
ObjectMapper objectMapper = new ObjectMapper();
EventList eventList = objectMapper.readValue(new File(&quot;C:\\Anurag\\Development\\CodeBase\\demo\\src\\main\\java\\com\\example\\demo\\domain\\testJson\\test.json&quot;), EventList.class);
List&lt;EventList.EventDetails&gt; eventsList = eventList.getEvents().getEventsList();
return eventsList;
}
}

映射Java类中的嵌套JSON值

huangapple
  • 本文由 发表于 2020年10月4日 08:10:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64190080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定