Reputation: 131
Input json:
{"id ": " 1"}
java class defineded:
class ID {
String id;
}
how to correctly get the ID object with "id = 1" instead of "id = null"
Upvotes: 1
Views: 2711
Reputation: 1
In my opinion, it is better to edit JSON beforehand and remove extra spaces.
var json = "{\"id \": \" 1\"}";
json = json.replaceAll("\\s+", "");
You'll get: {"id":"1"}
And then your POJO class will be correct.
After you can try to deserialize this json via Jackson. For example:
var id = mapper.readValue(json, ID.class);
Upvotes: 0
Reputation: 417
You may use @JsonProperty
annotation like this:
class ID {
@JsonProperty("id ")
String id;
public String getId() {
return id;
}
}
And then:
String id = myIdClassInstance.getId().trim();
But this will work only in cases where the number of spaces is fixed.
Also, you can access the fields of the object only at clearly defined names.
If you really want that "id"
and "id "
to point to the same field, you will need to format the JSON manually before sending it.
Upvotes: 3