2015-08-16 108 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.. 
       } 
      } 
     }