Vert.x Json.decodeValue list Vert.x Json.decodeValue list json json

Vert.x Json.decodeValue list


You'll have to declare a container object, but otherwise, it's quite simple:

// This is your Foopublic class MyObj {    public String key;    // Just for clarity    @Override    public String toString() {        return "MyObj{" +                "key='" + this.key + '\'' +                '}';    }}// This is the containerpublic class MyArray {   // Property is mandatory in this case   @JsonProperty("objs")   List<MyObj> objs;}

And now the parsing

public static void main(final String[] args) {    // Your input is a JSON array, not a JSON object    final String input = "[{\"key\":\"a\"}, {\"key\":\"b\"}, {\"key\":\"c\"}]";    // We format it to be a JSON object, so we can parse it    final MyArray res = Json.decodeValue(String.format("{\"objs\":%s}", input), MyArray.class);    // Get your List out    System.out.println(res.objs);}


Try this

List<Foo> fooList = Json.decodeValue(bodyAsString, new TypeReference<List<Foo>>(){});

TypeReference is Jackson reference for your information


The Json class use Jackson to decode the string.

I recommend you to use a Foo[] instead of a List. If you really need a List, you can easily create one "Arrays.asList(arr)".

Or you can use one of the examples on this site: http://www.baeldung.com/jackson-collection-array