Two ways of analyzing Json data by Gson

Keywords: JSON Java Google network

0x00 foreword

At present, there are many interfaces in the form of Json on the network. Gson is Google's open-source Json parsing library, which can easily convert Java objects into Json strings, and also can easily convert Json strings into Java objects.

0x01 two ways to parse Json string

Suppose that the string we want to parse is the following:

{
  "rst": 0,
  "msg": "ok",
  "data": {
    "cookie": "JSESSIONID=abcntKeuJhop56LGykfdw"
  }
}

Mode 1 (create a mapping class):

For the above data:
https://www.bejson.com/json2javapojo/new/
We can manually create the following classes or visit the above links to create the following Java entity classes:

JsonRootBean.java

public class JsonRootBean {

    private int rst;
    private String msg;
    private Data data;
    public void setRst(int rst) {
        this.rst = rst;
    }
    public int getRst() {
        return rst;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
    public String getMsg() {
        return msg;
    }

    public void setData(Data data) {
        this.data = data;
    }
    public Data getData() {
        return data;
    }

}

Data.java

public class Data {

    private String cookie;
    public void setCookie(String cookie) {
        this.cookie = cookie;
    }
    public String getCookie() {
        return cookie;
    }

}

Main class: Main.java

String json = "{\n" +
                "  \"rst\": 0,\n" +
                "  \"msg\": \"ok\",\n" +
                "  \"data\": {\n" +
                "    \"cookie\": \"JSESSIONID=abcntKeuJhop56LGykfdw\"\n" +
                "  }\n" +
                "}";
        JsonRootBean jsonRootBean = new Gson().fromJson(json, JsonRootBean.class);
        System.out.println("rst:" + jsonRootBean.getRst());
        System.out.println("msg:" + jsonRootBean.getMsg());
        System.out.println("data:" + jsonRootBean.getData().getCookie());

Result

rst:0
msg:ok
data:JSESSIONID=abcntKeuJhop56LGykfdw

Mode 2 (direct access):

Main class: Main.java

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Main {

    public static void main(String[] args) {
        String json = "{\n" +
                "  \"rst\": 0,\n" +
                "  \"msg\": \"ok\",\n" +
                "  \"data\": {\n" +
                "    \"cookie\": \"JSESSIONID=abcntKeuJhop56LGykfdw\"\n" +
                "  }\n" +
                "}";
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(json);
        System.out.println("rst:" + jsonObject.get("rst").getAsInt());
        System.out.println("msg:" + jsonObject.get("msg").getAsString());
        System.out.println("data:" + jsonObject.get("data").getAsJsonObject().get("cookie").getAsString());
    }
}

Result

rst:0
msg:ok
data:JSESSIONID=abcntKeuJhop56LGykfdw

0x02 conclusion

The two methods have their own advantages and disadvantages. The second method is suitable when there are few attributes to be used. Otherwise, the first method is suitable.

Posted by ronthu on Mon, 04 May 2020 04:42:15 -0700