Home > Software engineering >  json variable name starts with number
json variable name starts with number

Time:11-16

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

CodePudding user response:

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"));

CodePudding user response:

You can use Jackson Custom Serialization/Deserialization

And register it to the class level

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