使用未知密钥反序列化JSON

时间:2015-08-16 00:04:59

标签: java json jackson deserialization

我正在尝试使用未知密钥反序列化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;
    ...
}

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

2 个答案:

答案 0 :(得分:2)

如果您事先不知道密钥,则无法定义相应的字段。你能做的最好的就是使用Map<String,Object>

如果实际上有一些类型,您可以为其识别字段集合,则可以编写自定义反序列化程序来检查字段并返回相应类型的对象。

答案 1 :(得分:2)

我知道这是一个古老的问题,但我也遇到了问题并且有结果...... Meybe将来会帮助某人:)

我对未知密钥的回复:

in Model Class
private JsonElement attributes;


"attributes": {
        "16": [],
        "24": {
          "165": "50000 H",
          "166": "900 lm",
          "167": "b.neutr.",
          "168": "SMD 3528",
          "169": "G 13",
          "170": "10 W",
          "171": "230V AC / 50Hz"
        }
      },

所以我还检查了jsonElement是否为jsonArray为空。 如果是jsonObject,我们有数据。

  ProductModel productModel = productModels.get(position);

        TreeMap<String, String> attrsHashMap = new TreeMap<>();

        if (productModel.getAttributes().isJsonObject())
        {
         for (Map.Entry<String,JsonElement> entry : productModel.getAttributes().getAsJsonObject().entrySet())
         {
             Log.e("KEYS", "KEYS: " + entry.getKey() + " is empty: " + entry.getValue().isJsonArray());

             if (entry.getValue() != null && entry.getValue().isJsonObject())
             {
                 for (Map.Entry<String, JsonElement> entry1 : entry.getValue().getAsJsonObject().entrySet())
                 {
                     Log.e("KEYS", "KEYS INSIDE: " + entry1.getKey() + " VALUE: " + entry1.getValue().getAsString());

            // and there is my keys and values.. in your case You can get it in upper for loop..
                 }
             }
         }