Gson 解析動態 Key

出自Silica Library | 間奏時光
跳至導覽 跳至搜尋

2021-08-02 16:36:41

舊文章

  Silica Blog

要解析的 json

{
    "sessions": {
        "19656": {
            "bh_files": {
                "slides": {
                    "url": "http://i.blackhat.com/asia-20/Thursday/asia-20-Kruger-WiFi-Brokering.pdf"
                },
                "whitepaper": {
                    "url": "http://i.blackhat.com/asia-20/Thursday/asia-20-Kruger-WiFi-Whitepaper.pdf"
                },
                "source_code": {
                    "url": "https://github.com/sensepost/wpa_sycophant"
                }
            }
        }
    }
}

目標

  19656 為動態 Key,目標是拿到所有 url 的 Value。

Bean.java

  根 bean。由於動態 Key 就在 sessions 下面,所以用 Map 來解析 sessions 的內容。

public class Bean {
    public Map<String, JsonElement> sessions;
}

Bean2.java

  子 bean。由於內容就在動態 Key 上面,裡面已經沒有動態 Key 了,所以裡面的 bean 照常用 GsonFormat 生成即可。

public class Bean2 {

    private BhFilesBean bh_files;

    public BhFilesBean getBh_files() {
        return bh_files;
    }

    public static class BhFilesBean {
        private SlidesBean slides;
        private WhitepaperBean whitepaper;
        private SourceCodeBean source_code;

        public SlidesBean getSlides() {
            return slides;
        }

        public WhitepaperBean getWhitepaper() {
            return whitepaper;
        }

        public SourceCodeBean getSource_code() {
            return source_code;
        }

        public static class SlidesBean {
            private String url;
            public String getUrl() {
                return url;
            }
        }

        public static class WhitepaperBean {
            private String url;
            public String getUrl() {
                return url;
            }
        }

        public static class SourceCodeBean {
            private String url;
            public String getUrl() {
                return url;
            }
        }
    }
}

解析

Bean bean = null;
try {
    bean = new Gson().fromJson(data, Bean.class);
} catch (JsonSyntaxException e) {
    e.printStackTrace();
}
StringBuilder builder = new StringBuilder();
for (String key : bean.sessions.keySet()) {
    Log.d("NeneLog", key);
    Bean2 bean2;
    try {
        bean2 = new Gson().fromJson(bean.sessions.get(key), Bean2.class);
    } catch (JsonSyntaxException e) {
        Log.d("NeneLog", "解析失败");
        continue;
    }
    if (bean2.getBh_files().getSlides().getUrl().length() > 0) {
        if (!bean2.getBh_files().getSlides().getUrl().contains("github")) {
            builder.append(bean2.getBh_files().getSlides().getUrl()).append("\n");
        }
    }
    if (bean2.getBh_files().getSource_code().getUrl().length() > 0) {
        if (!bean2.getBh_files().getSource_code().getUrl().contains("github")) {
            builder.append(bean2.getBh_files().getSource_code().getUrl()).append("\n");
        }
    }
    if (bean2.getBh_files().getWhitepaper().getUrl().length() > 0) {
        if (!bean2.getBh_files().getWhitepaper().getUrl().contains("github")) {
            builder.append(bean2.getBh_files().getWhitepaper().getUrl()).append("\n");
        }
    }
}
etResult.setText(builder.toString());