Gson 解析动态 Key

来自Silica Library | 間奏時光
Silica讨论 | 贡献2021年8月2日 (一) 08:45的版本 (创建页面,内容为“Category:Android 2021-08-02 16:36:41 == 旧文章 ==   [https://blog.silica.moe/2017/05/24/%e4%bd%bf%e7%94%a8-gson-%e8%a7%a3%e6%9e%90%e5%b8%a6%e6%9c%89%e5%8…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

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());