0

Some api need json like this:

{
  "token_123213p123": ""
}

the token_123213p123 is dynamic, so I create a class:

@Data
public class Entity {
  // I will set 'token_123213p123' in this
  @JsonIgnore
  private String token;
}

so how to when serialize to add "token_123213p123": "" from token

ztcaoll22
  • 1
  • 1

1 Answers1

0

Use a Map<>:

static class Data {
    public Map<String, String> map;
}

public static void main(String[] args) throws JsonProcessingException {
    Data data = new ObjectMapper().readValue("{\"map\": {\"foo\": \"bar\", \"bar\": \"foo\"}}", Data.class);
    data.map.forEach((k, v) -> System.out.printf("%s: %s%n", k, v));
}

with output

foo: bar
bar: foo
josejuan
  • 9,338
  • 24
  • 31