Search code examples
javajsonstruts2

Change key name in bean to JSON conversion dynamically


I have a entity bean

@Entity
@Table(name="info")
public class Info{    

    @Column(name="name", nullable = false)
    private String name;
}

I am using this bean with hibernate to store the object value in database. However I have a requirement to convert the bean as JSON like this

{
  "param1":"AB_9999"
}

instead of

{
   "name":"AB_9999"
}

I don't want to change the column name but also want the JSON should have 'param1' instead of 'name' as key.

I can already convert the bean to JSON only thing I wish to know is,

How to change the key name dynamically ?


Solution

  • Please try:

    @Entity
    @Table(name="info")
    public class Info{    
    
        @Column(name="name", nullable = false)
        private String name;
    
        @JSON(name="param1") //I added this
        public String getName(){return name;}
    
    }