Post

Gson 파싱시 Underscore를 CamelCase로 변환

1. Underscore를 CamelCase로 변환하여 Json 객체로 변환

  • JsonObject.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import pbb.ga.subscriber.service.JsonVO;

public class JsonObject {

    public static void main(String[] args) {
        JsonObject gson = new JsonObject();
        gson.parsing("{\"cntlr_id\":\"3001\",\"pnl_tp\":\"20.99552\",\"pnl_hd\":\"52.51834\"" +
                ",\"colctdate\":\"2020-06-30 00:00:00\",\"cntlr_mode\":\"Manual\"}");
    }

    private void parsing(String jsonData) {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
        JsonVO jsonVO = gson.fromJson(jsonData, JsonVO.class);

        System.out.println("cntlrId : " + jsonVO.getCntlrId());
        System.out.println("pnlTp : " + jsonVO.getPnlTp());
        System.out.println("pnlHd : " + jsonVO.getPnlHd());
        System.out.println("colctdate : " + jsonVO.getColctdate());
        System.out.println("cntlrMode : " + jsonVO.getCntlrMode());
    }
}
  • JsonVO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class JsonVO {

    private String cntlrId;
    private String pnlTp;
    private String pnlHd;
    private String colctdate;
    private String cntlrMode;

    public String getCntlrId() {
        return cntlrId;
    }

    public void setCntlrId(String cntlrId) {
        this.cntlrId = cntlrId;
    }

    public String getPnlTp() {
        return pnlTp;
    }

    public void setPnlTp(String pnlTp) {
        this.pnlTp = pnlTp;
    }

    public String getPnlHd() {
        return pnlHd;
    }

    public void setPnlHd(String pnlHd) {
        this.pnlHd = pnlHd;
    }

    public String getColctdate() {
        return colctdate;
    }

    public void setColctdate(String colctdate) {
        this.colctdate = colctdate;
    }

    public String getCntlrMode() {
        return cntlrMode;
    }

    public void setCntlrMode(String cntlrMode) {
        this.cntlrMode = cntlrMode;
    }
}

[출처 및 참고]

This post is licensed under CC BY 4.0 by the author.