Search code examples
jsonjsonserializer

convert JSON Object to Java bean


How can i convert a json objet to java bean(MainBean) of following type

class MainBean{
private int id;
private SubBean sBean;
}  
class SubBean{
private String name;
private List<DBean> dBean;
}

Solution

  • There are alot of libraries that can do it.
    For exanple, you can do it using Google-Gson

    MainBean bean = new Gson().fromJson(yourJsonAsString, MainBean.class); 
    

    JSON like this will be converted well

    {"id" : 1, "sBean" : {"name" : "beanname", "dBean" : []}}