Java 反序列化具有关键变量jackson map ObjectMapper的JSON文件
我有这样的JSON:Java 反序列化具有关键变量jackson map ObjectMapper的JSON文件,java,android,jackson,mapper,Java,Android,Jackson,Mapper,我有这样的JSON: { "flux":{ "1400364000":{ "Optaf_matchdata":{ "uid":"749674", "fk_comp_id":"2414", "comp_id":"112", "saisonid":"2013", "temps":null, "matchday":"40" } }, "1400104800":{
{
"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"
}
}
}
}
问题是键1400364000
和1400104800
是可变的。当我没有名字的时候,我怎么能把它放在一个@JsonProperty
我可以分别检索那些密钥1400364000和1400104800。例如,如何从
1400364000
键中检索Optaf\u matchdata
?更新:我刚刚意识到您使用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_匹配数据 我不确定我是否理解你的问题,但这是我试图回答的问题 您可以使用
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的键):
我通过如下方式添加客户反序列化来解决此问题:
@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;
}
@nikis你在JSON中添加了一个缺少的大括号,也许应该指出这一点。可能我只是试图保留JSON中引起我问题的重要部分,我删除了所有其他部分,这是完整的JSON www.thefanclub.com/appsfootball/calendrierbyteam/241/0/active.ijsonOK,原来的JSON似乎是正确的,你可能已经删除了这个cur在切掉JSON中不相关的部分时使用大括号。您是在尝试访问所有键(如“1400364000”)的字段,还是要访问运行时知道的某些特定键的字段?您是否控制此JSON?
@JsonIgnoreProperties(ignoreUnknown = true)
public class CalendarResultsDto {
@JsonDeserialize(using=JsonMatchDeserialize.class)
@JsonProperty(value="flux")
@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;
}
public class CalendarMatchDataDto {
@JsonProperty(value="Optaf_matchdata")
public MatchDto matchDto ;
}