是否可以使用 Jackson
在 Java 中为 json
属性创建某种掩码?
我的json
有很多类似的字段
"records":[
{
"ShippingStreet":null,
"ShippingState":null,
"ShippingPostalCode":null,
"ShippingCountry":null,
"ShippingCity":null,
"BillingStreet":null,
"BillingState":null,
"BillingPostalCode":null,
"BillingCountry":null,
"BillingCity":null,
我必须创建两个对象 - 一个用于 Shipping
字段,一个用于 Billing
字段。以下是 BillingAddress
public class BillingAddress {
@JsonProperty("BillingStreet")
private String billingStreet;
@JsonProperty("BillingState")
private String billingState;
@JsonProperty("BillingPostalCode")
private String billingPostalCode;
@JsonProperty("BillingCountry")
private String billingCountry;
@JsonProperty("BillingCity")
private String billingCity;
而且我必须为 Shipping
字段创建几乎相同的对象。
结果,我的 json
java 对象有两个类型不同但功能几乎相同的字段
@JsonIgnoreProperties(ignoreUnknown=true)
public class Account {
@JsonUnwrapped
private BillingAddress billingAddress;
@JsonUnwrapped
private ShippingAddress shippingAddress;
是否可以创建某种掩码,我不需要两种类型的对象,而是使用一种类型,但定义它们使用的字段类型。例如这样的事情。 一个对象
public class Address {
@JsonProperty("*Street")
private String street;
@JsonProperty("*State")
private String state;
@JsonProperty("*PostalCode")
private String postalCode;
@JsonProperty("*Country")
private String country;
@JsonProperty("*City")
private String city;
并且两个字段类型相同
@JsonIgnoreProperties(ignoreUnknown=true)
public class Account {
@JsonUnwrapped
@Annotation("Billing*")
private Address billingAddress;
@JsonUnwrapped
@Annotation("Shipping*")
private Address shippingAddress;
最佳答案
非常具有挑战性的问题。我已经试过了,我的输出是
“记录”:[
{“账户城市”:“CHE”,“账户状态”:“TN”}
{“Billingcity”:“TRI”,“Billingstate”:“TN”}]
也许你可以引用一下。如果有的话请告诉我。
服务端:
@GET
@Path("bil")
@Produces(MediaType.APPLICATION_JSON )
public Response sendBill(@HeaderParam("Accept") String accept) throws JsonGenerationException, JsonMappingException, IOException{
Records records = new Records();
Account account = new Account();
Billing billing = new Billing();
Address address1 = new Address("CHE","TN");
Address address3 = new Address("TRI","TN");
account.setAccountAddress(address1);
billing.setBillingAddress(address3);
records.setAccount(account);
records.setBilling(billing);
return Response.ok(convertToJSON(records), accept).build();
}
private String convertToJSON(Records records) throws JsonGenerationException, JsonMappingException, IOException{
ObjectMapper mapper = new ObjectMapper();
convert(mapper,"Account");
String account = mapper.writeValueAsString(records.getAccount());
System.out.println(account);
ObjectMapper mapper1 = new ObjectMapper();
convert(mapper1,"Billing");
String billing = mapper1.writeValueAsString(records.getBilling());
System.out.println(billing);
System.out.println("\"records\":["+account+ billing + "]");
return "\"records\":["+account+ billing + "]";
}
private void convert(ObjectMapper mapper,String prefix){
Map<String,String> map = new LinkedHashMap<>();
map.put("city",prefix+"city");
map.put("state",prefix+"state");
mapper.setPropertyNamingStrategy(new ReplaceNamingStrategy(map));
}
关于java - java对象中json属性的掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29005592/