简体   繁体   English

将Java对象转换为具有不同对象名称的json

[英]convert java object to json with different object name

I want to convert java object to JSON with different object name. 我想将Java对象转换为具有不同对象名称的JSON。 For Example : 例如 :

public class MetaProfileResponse {

    private Boolean tour ;

    private Integer maxFreeUser;

    private Boolean paid;

    private Integer status;
}

other Class is : 其他类是:

public class ProfileResponse {

    private String domainName;

    private String companyName;

    private String country;

    private String postCode;
}

A class have object of both class : 一个类具有两个类的对象:

public class GetProfileResponse {

    private MetaProfileResponse metaProfileResponse;

    private ProfileResponse profileResponse;
}

when we get response as JSON We got : 当我们以JSON格式获得响应时,我们得到:

{"metaProfileResponse":{"tour":null,"maxFreeUser":25,"paid":false,"status":0},"profileResponse":{"domainName":null,"companyName":null,"country":null,"postCode":null}} {“ metaProfileResponse”:{“ tour”:null,“ maxFreeUser”:25,“ paid”:false,“ status”:0},“ profileResponse”:{“ domainName”:null,“ companyName”:null,“ country “:null,” postCode“:null}}

But We want result as : {"meta":{"tour":null,"maxFreeUser":25,"paid":false,"status":0},"profile":{"domainName":null,"companyName":null,"country":null,"postCode":null}} 但是我们希望结果为:{“元”:{“游览”:null,“ maxFreeUser”:25,“付费”:false,“状态”:0},“配置文件”:{“域名”:null,“ companyName “:null,” country“:null,” postCode“:null}}

without changing class name 无需更改班级名称

You can use the annotation @JsonProperty(value="whateverValueYouWant") on the getter of metaProfileResponse; 您可以在@JsonProperty(value="whateverValueYouWant")方法上使用注释@JsonProperty(value="whateverValueYouWant") metaProfileResponse; It should give you proper result. 它应该给您适当的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM