java - 使用 GSON 传递 jira json

标签 java json gson jira

除了这个给我带来麻烦外,我已经使用 GSON 解析了我的所有数据。

我有像这样的 JSON 数据(对于 Issue java pojo):

    "issue": {
    "id": "44544",
    "self": "http://jira.webaroo.com/rest/api/2/issue/44544",
    "key": "BIZSOL-166",
    "fields": {
        "summary": "Jira Wrapper Implementation - Test",
        "issuetype": {
            "self": "http://jira.webaroo.com/rest/api/2/issuetype/2",
            "id": "2",
            "description": "A new feature of the product, which has yet to be developed.",
            "iconUrl": "http://jira.webaroo.com/images/icons/issuetypes/newfeature.png",
            "name": "New Feature",
            "subtask": false
        },
        "votes": {
            "self": "http://jira.webaroo.com/rest/api/2/issue/BIZSOL-166/votes",
            "votes": 0,
            "hasVoted": false
        },
        "resolution": null,
        "fixVersions": [],
        "resolutiondate": null,
        "customfield_11101": null
    }
}

我的 java 类是 Issue.java :

protected String key;
protected String summary;
protected IssueType issuetype;
protected Votes votes;
protected Resolution resolution;
protected List<FixVersions> fixVersions;
protected Date resolutiondate;

我能够从 GSON 转换中获取键值。

但我无法获取其他数据。

我知道它不会出现,因为它们是“字段”结构的一部分,但我不想在我的 java 中定义“字段”结构。

我直接想获取下一级的值。

请帮助我使用 GSON 实现这一目标。我是 GSON 的新手。

最佳答案

也许有点晚了,但对于 WIT

可以将json解析成json对象,然后通过键名获取元素。

public static void main(String[] args) throws FileNotFoundException {
Gson gson = new Gson();
JsonParser prser = new JsonParser();
JsonReader file = new JsonReader(new FileReader("./file.txt"));
JsonObject result = prser.parse(file).getAsJsonObject();
System.out.println(result.get("issue").getAsJsonObject().get("id"));
System.out.println(result.get("issue").getAsJsonObject().get("key"));
System.out.println(result.get("issue").getAsJsonObject().get("fields").getAsJsonObject().get("votes")
    .getAsJsonObject().get("self"));
}

结果将是:

"44544"

"BIZSOL-166"

"http://jira.webaroo.com/rest/api/2/issue/BIZSOL-166/votes"

您唯一需要注意的是嵌套键... 例子: id 是来自issue 的子项,因此您必须获得第一个父项并深入导航,直到找到您想要的元素

您肯定可以将集合定义为:

Set<Map.Entry<String, JsonElement>> entrySet = result.entrySet(); 

关于java - 使用 GSON 传递 jira json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28983723/

相关文章:

java - 创建表后立即插入行 - Oracle

java - ThreadLocal 的使用

java - 消息驱动 Bean - 连续循环

c# - JsonConverter 等效于使用 System.Text.Json

android - 如果模型的属性是私有(private)的,则 Retrofit Android 无法将 JSON 响应转换为模型

java - Gson 和 CSV 冲突?

java - 使用瘦驱动程序的 jdbc 连接

ios - 使用 Swift 解析 tableview 中的 Json

java - 通过 gson 反序列化 json 文件时遇到问题

java - 在 Android Studio 中保存 Arraylist(Gson,或 Storage)