0
0

More than 1 year has passed since last update.

JSONオブジェクトをBeanに変換する

Posted at

APIを使用して取得してきたJSON形式のデータを型変換して、Beanに格納する処理についてです。
インタフェースを通して外部システムからデータを取得して、そのデータをBeanに格納するために、データをBeanの型に変換する必要があります。
スクリーンショット 2023-02-18 8.55.11.png

前提

外部システムから取ってきたデータは以下のJSON形式です。

{"catList":[{"id":"1",name:"トラ吉","age":"3"}]}

クラスはAnimalBeanクラスと、その子クラスであるCatBeanクラス。AnimalBeanの中にCatBean型のListがあります。

AnimalBean.java


public class AnimalBean {

    private List<CatBean> catList = null;

    public List<CatBean> getCatList() {
        return catList;
    }

    public void setCatList(List<CatBean> catList) {
        this.catList = catList;
    }
}

CatBean.java

便宜上全てString型で定義


public class CatBean {

    private String id = null;

    private String name = null;

    private String age = null;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAge() {
        return age;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

JSONのデータはCatBeanクラスで定義した変数名と一致します。
CatBeanクラスの変数の中にJSONのデータが入り、AnimalBeanクラスで定義しているcatListに、id,name,ageが格納される流れです。

JSONObject.toBean

JSONObejectクラスに用意されているJSONオブジェクトをBeanに変換してくれるメソッド。

JSONObject.toBean(変換したいJSONオブジェクト,変換先クラス);

JSONConfig

JSONオブジェクトをBeanに変換するときにBeanやリストの型を設定するクラス。
変換先Beanの子項目に型を設定するときに使用する。
今回はAnimalBeanのcatListにデータを格納したいので、外部システムから取得したデータをcatListの型に変換する必要がある。そのときcatListに変換先の型を指定する必要がある。
以下のように設定するだけでは中身の項目(catList)の型を指定することはできない。

JSONObjectBean(前提で記述したJSONオブジェクト,AnimalBean.class);

そこでJSONConfigを使用する。

private JsonConfig createConfig() {
    JsonConfig jsonConfig = new jsonConfig();
    jsonConfig.setRootClass(AnimalBean.class); //親クラスの指定
    Map<String,Class> classMap = new HashMap<String,Class>();
    classMap.put("catList",CatBean.class); //親クラスに格納されているcatListの型を設定
    jsonConfig.setClassMap(classMap); //jsonConfingにセット
    return jsonConfig
}

JsonConfig jsonConfig = createConfig();
AnimalBean res = (AnimalBean)JSONObject.toBean(変換したいJsonオブジェクト,jsonConfig);

これでcatListがCatBean型になる。

全体的な処理


public AnimalBean getCatInfo(省略) {
    String res = APIを使用して外部システムからJSON形式のデータをString型で受け取るメソッドの呼び出し(前提で記述したデータが返ってくる)

    Map<String,Class> classMap = new HashMap<String,Class>();
    classMap.put("catList",CatBean.class); //親クラスに格納されているcatListの型を設定
    JsonConfig jsonConfig = new jsonConfig();
    jsonConfig.setRootClass(AnimalBean.class); //親クラスの指定
    jsonConfig.setClassMap(classMap); //jsonConfingにセット

    AnimalBean resBean = (AnimalBean)JSONObject.toBean(res,jsonConfig);

    return resBean
}

これでAnimalBeanのcatListに外部システムから取得したデータを格納することができます。

終わり

外部システムからデータを取得してBeanに格納する処理を簡単に書きました。
今の案件は外部システムから情報を取得する事が多いので、これからもよく使いそうです。
何かご指摘あればお願いいたします!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0