简体   繁体   中英

How to parse JSON data array with random key names using Java Spring Webclient?

I am new to Spring Boot and APIs in general. I am having trouble parsing with parsing a JSON Get response using Java-Spring's WebClient where I have something like

client.get().uri("/path")
                .retrieve()
                .bodyToFlux(TimeTable.class)
                .collectList()
                .block();

where I have a class that with @JsonProperty that specifies the key of the key-value pair but I don't know how to parse it if the fields have random identifier names rather than a set key every time. It works when I know what the key value is going to be, but for random ones, I'm not quite sure how to approach it using WebClient .

[{
        "4f471d13-8842-45ba-8f3b-2f7fd0977b47PlaceA": "09:35 AM",
        "6edc3d0a-ce4e-4c02-a4a8-425fbf77d509PlaceB": "09:43 AM",
        "5e4bf15a-f858-479c-b9c3-7ce8b32322ddPlaceC": "09:45 AM",
        "2bd5bd43-8aef-49d8-8cba-f999cba41ec6PlaceD": "09:47 AM",
        "ebb3e72a-fa1f-4299-87b8-4dd099369d11PlaceE": "09:55 AM"
},

... //and so on ]

I've looked into some things like @JsonIgnoreProperties or annotations but I can't find anything that deals with WebClient and the Mono and Flux things I can't get a grasp of it to make sense of it.

The following works:

List<Map<String, String>> list = 
     objectMapper.readValue(new File("your-json.json"), new TypeReference<>(){});

So the following should work with your WebClient :

client.get().uri("/path")
                .retrieve()
                .bodyToFlux(new ParameterizedTypeReference<Map<String, String>>() {})
                .collectList()
                .block();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM