2015-08-16 95 views
0

我试图反序列化JSON对象(来自JIRA REST API createMeta)与未知的键。反序列化JSON与未知键

{ 
"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" 
         ] 
        } 
       } 
      } 
     ] 
    } 
] 

}

我的问题是:我不知道键进入 “田”(以下 “问题类型”, “摘要”, “描述”, “customfield_12345” 的例子)。

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

这将是真棒,如果我能反序列化与我的POJO的关键是“ID”阵列,使得上面的例子looke像下面这样:

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

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

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

1

我知道这是老问题,但我也有问题,这一点,并有结果.. 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是否为空。 如果是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.. 
       } 
      } 
     }