gpt4 book ai didi

java - 使用未知 key 反序列化 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:29 25 4
gpt4 key购买 nike

我正在尝试使用未知 key 反序列化 JSON 对象(来自 JIRA REST API createMeta )。

{
"expand": "projects",
"projects": [
{
"self": "http://www.example.com/jira/rest/api/2/project/EX",
"id": "10000",
"key": "EX",
"name": "Example Project",
"avatarUrls": {
"24x24": "http://www.example.com/jira/secure/projectavatar?size=small&pid=10000&avatarId=10011",
"16x16": "http://www.example.com/jira/secure/projectavatar?size=xsmall&pid=10000&avatarId=10011",
"32x32": "http://www.example.com/jira/secure/projectavatar?size=medium&pid=10000&avatarId=10011",
"48x48": "http://www.example.com/jira/secure/projectavatar?pid=10000&avatarId=10011"
},
"issuetypes": [
{
"self": "http://www.example.com/jira/rest/api/2/issueType/1",
"id": "1",
"description": "An error in the code",
"iconUrl": "http://www.example.com/jira/images/icons/issuetypes/bug.png",
"name": "Bug",
"subtask": false,
"fields": {
"issuetype": {
"required": true,
"name": "Issue Type",
"hasDefaultValue": false,
"operations": [
"set"
]
}
}
}
]
}
]

}

我的问题是:我不知道“字段”的键(在下面的示例中为“issuetype”、“summary”、“description”、“customfield_12345”)。

"fields": {
"issuetype": { ... },
"summary": { ... },
"description": { ... },
"customfield_12345": { ... }
}

如果我可以将它反序列化为一个数组,并且在我的 POJO 中将其作为键作为“id”,那就太棒了,所以上面的示例将如下所示:

class IssueType {
...
public List<Field> fields;
...
}

class Field {
public String id; // the key from the JSON object e.g. "issuetype"
public boolean required;
public String name;
...
}

有没有办法可以实现这一点并包装在我的模型中?我希望我的问题可以理解:)

最佳答案

如果您事先不知道键,则无法定义适当的字段。您能做的最好的事情就是使用 Map<String,Object>

如果实际上存在少数类型,您可以为其识别字段集合,则可以编写自定义反序列化器来检查字段并返回适当类型的对象。

关于java - 使用未知 key 反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32030571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com