我有一些json的名字中有下划线,这些下划线来自aa第三方API,所以我无法更改它们。但是,我希望使用不带下划线的bean属性名。下面是一个JSON示例,它表示其中一个对象。这是JSON,我想Map到一个名为杂志的POJO。但是,当我将杂志对象转换回JSON时,JSON中会出现下划线。我怎样才能得到没有下划线名称的输出?我希望能够使用不带下划线的bean名称,比如在POJO中使用name而不是_name。
{
"_name": "Vogue",
"authors": ["Jenny Mason", "Craig Hall"],
"_edition": "plastic-surgery",
"_unit_price": "!.75",
"_publication_date": "03/22/2023"
}
以下是Jackson对象Map器配置:
public ObjectMapper initMapper(){
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
return mapper;
}
下面是将JSONMap到的对象杂志对象:
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;
@Data
public class Magazine {
@JsonProperty("name")
private String _name;
@JsonProperty("authors")
private List<String> _authors;
@JsonProperty("unitPrice")
private String _unitPrice;
@JsonProperty("publicationDate")
private String _publication_date;
@JsonProperty("edition")
private String _edition;
public Magazine(String _name, List<String> _authors, String _edition,
String _unitPrice, String _publication_date) {
this._name = _name;
this._authors = _authors;
this._edition = _edition;
this._unitPrice = _unitPrice;
this._publication_date = _publication_date;
}
}
1条答案
按热度按时间qcuzuvrc1#
我可以使用@JsonSetter和@JsonGetter解决这个问题。举个例子