2014-04-30 19 views
1

我JSON像這樣的JSON文件:反序列化,有一個關鍵變量 - 傑克遜地圖ObjectMapper

{ 
    "flux":{ 
     "1400364000":{ 
      "Optaf_matchdata":{ 
       "uid":"749674", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"40" 
      } 
     }, 
     "1400104800":{ 
      "Optaf_matchdata":{ 
       "uid":"749670", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"39" 
      } 
     } 
    } 
} 

的問題是,按鍵14003640001400104800是可變的。我怎麼能把這個放在@JsonProperty,當我沒有名字?

我可以單獨檢索那些密鑰1400364000,1400104800。例如,如何從1400364000密鑰中檢索Optaf_matchdata

+0

@nikis您添加的JSON缺少大括號,也許這應該指出出。 – Joffrey

+0

也許我嘗試僅保留的Json的重要組成部分,引起了我的問題,我刪除所有其他部分 以下是完整的JSON www.thefanclub.com/appsfootball/calendrierbyteamid/241/0/active.ijson – tamtoum1987

+0

OK,原來的JSON看起來不錯,你可能在刪除JSON的不相關部分的同時刪除了大括號。 – Joffrey

回答

2

我解決此通過添加客戶反序列化這樣的:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class CalendarResultsDto { 
@JsonDeserialize(using=JsonMatchDeserialize.class) 
@JsonProperty(value="flux") 

所以返回一個地圖魔女字符串之後,這是關鍵。 JsonMatchDeserialize.java:

公共類JsonMatchDeserialize擴展JsonDeserializer> {

@Override 
public Map<String, CalendarMatchDataDto> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) 
     throws IOException, JsonProcessingException { 

    Map<String, CalendarMatchDataDto> mapToReturn = new HashMap<String, CalendarMatchDataDto>(); 
    JSONObject flux; 
    try { 
     flux = new JSONObject(jsonParser.getValueAsString()); 
     ObjectMapper mapper = new ObjectMapper(); 
     Iterator<String> iter = flux.keys(); 
      while (iter.hasNext()) { 
       String key = iter.next(); 
       try { 
        Object value = flux.get(key); 
        CalendarMatchDataDto calendarMatchDataDto = mapper.readValue(value.toString(), CalendarMatchDataDto.class); 
        if(key!=null && calendarMatchDataDto!=null) 
         mapToReturn.put(key, calendarMatchDataDto); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
    } catch (JSONException e1) { 
     e1.printStackTrace(); 
    } 




    return mapToReturn; 



} 

和CalendarMatchDataDto.java

public class CalendarMatchDataDto { 

@JsonProperty(value="Optaf_matchdata") 
public MatchDto matchDto ; 
     } 
0

更新:我剛剛意識到您正在使用Jackson自動轉換您的JSON,而不是像我建議的那樣手動解析它。我之前的回答可能根本不適合您的需求。

我的評論會是:那些可變密鑰是什麼?它們可能應該是一個名爲「id」的鍵或其他東西的值。

另外,您應該考慮使用JSON數組。

這是一個什麼樣的JSON或許應該像,而不是一個例子:

{ 
    "flux":[ 
     { 
     "id":"1400364000", 
     "Optaf_matchdata":{ 
      "uid":"749674", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"40" 
     } 
     }, 
     { 
     "id":"1400104800", 
     "Optaf_matchdata":{ 
      "uid":"749670", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"39" 
     } 
     } 
    ] 
} 

原來的答案

如何從例如該鍵1400364000得到他們Optaf_matchdata

不知道我理解你的問題,但這是我的回答。

可以使用getJSONObject()方法:

JSONObject source = new JSONObject(jsonString); 
JSONObject flux = source.getJSONObject("flux"); 
JSONObject item = flux.getJSONObject("1400364000"); // this string can be dynamic 
JSONObject optafMatchData = item.getJSONObject("Optaf_matchdata"); 

你甚至可以重複上flux對象的所有鍵(在1400364000樣鍵):

JSONObject source = new JSONObject(jsonString); 
JSONObject flux = source.getJSONObject("flux"); 
Iterator<?> keys = flux.keys(); 
while(keys.hasNext()){ 
    String key = (String) keys.next(); 
    JSONObject item = flux.getJSONObject(key); 
    JSONObject optafMatchData = item.getJSONObject("Optaf_matchdata"); 
    // do whatever with this item/match data 
} 

這時,你可能想要寫一種將從JSON中解析對象OptafMatchData的方法:

private static OptafMatchData parseMatchData(JSONObject item) throws JSONException { 
    OptafMatchData omd = new OptafMatchData(); 
    omd.setUid(item.getLong("uid")); 
    omd.setFkCompId(item.getLong("fk_comp_id")); 
    omd.setCompId(item.getLong("comp_id")); 
    // and so on... 
    return omd; 
}