1

I want to convert java object to JSON with different object name. 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 :

{"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}}

without changing class name

4
  • Where you're using this objects? In REST? Commented Feb 19, 2015 at 9:57
  • Yes we are using in spring restful services Commented Feb 19, 2015 at 10:00
  • Try by using @XmlRootElement(name="meta") annotation in MetaProfileResponse Commented Feb 19, 2015 at 10:03
  • using that annotation we are getting same response. Commented Feb 19, 2015 at 10:10

1 Answer 1

2

You can use the annotation @JsonProperty(value="whateverValueYouWant") on the getter of metaProfileResponse; It should give you proper result.

Sign up to request clarification or add additional context in comments.

2 Comments

For this you need jackson core and jackson mapper jars
Which ways do I have to do this, if I am using objects from third party lib?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.