json variable name starts with number

412 views Asked by At

example json

{
"11var":"value1",
"11var2":"val2",
"11var3":"val3",
"11var4":"val444",
"11var5":"val5",
.....
}

how to convert this to a pojo in latest spring boot and jackson setup?

PS: I know we can do @JsonProperty("11var") and so on for all variables. my point what are the other ways. and also main issue here we cant start variable names with numbers in java check here

2

There are 2 answers

0
dev_in_progress On

Use map and jackson objectMapper

String json = "{\"11var\":\"value1\",\"11var2\":\"val2\",\"11var3\":\"val3\",\"11var4\":\"val444\",\"11var5\":\"val5\"}";

ObjectMapper objectMapper = new ObjectMapper();

Map<String, Object> map = objectMapper.readValue(json, Map.class);
System.out.println(map.get("11var"));
0
so-random-dude On

You can use Jackson Custom Serialization/Deserialization

And register it to the class level

@JsonSerialize(using = ItemSerializer.class)
public class Item {
    ...
}