我目前正在尝试优化java中的json解析 是不是可以从头到尾解析一个Json?
我使用faster-xml / jackson-core(2.2.3)库来解析以下json对象 这些实际上是按年份分组的嵌套对象,然后是月份。 请参阅下面的示例,有些年份只有几个月相关,所以其他几个省略。 json对象是我在Java中解析的网站的一部分。 假设我只想解析当前年份(2014年)下降的最新5个订单。 问题:json对象是按升序排列的,所以要获得最新的5个entr,我必须从头到尾通过整个json对象。
有没有办法反过来做?或者重新加载json对象? 其他想法可以解决这个问题吗?
var calendarGroupYearMonth = $.parseJSON('{"2000":{"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2001":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2002":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2003":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2004":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2005":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2006":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2007":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2008":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2009":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2010":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2011":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2012":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2013":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2014":{"1":1,"2":1,"3":1}}');
我不希望它作为对象结构,所以我通过令牌解析它令牌 这是我的json解析代码,工作正常。
JsonParser parser = jf.createParser(jsonSTring);
JsonToken currentToken = parser.nextToken();
String month = "";
String year = "";
final int YEAR_LENGTH = 4;
while (parser.hasCurrentToken()) {
if (currentToken == JsonToken.FIELD_NAME) {
String text = parser.getText();
if (text.length() == YEAR_LENGTH) {
year = text;
} else {
month = text;
}
//Do some stuff with the year and month
}
currentToken = parser.nextToken();
答案 0 :(得分:0)
您只需使用@JsonAnySetter
注释即可。您的POJO
课程应如下所示:
class Pojo {
private static final int MAX_SIZE = 5;
private TreeMap<Integer, Map<String, String>> yearsMap = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer value1, Integer value2) {
return value2.compareTo(value1);
};
});
@JsonAnySetter
public void setMonthFromLastYear(String year, Map<String, String> value) {
Integer currentYear = Integer.valueOf(year);
if (yearsMap.size() <= MAX_SIZE) {
yearsMap.put(currentYear, value);
} else if (currentYear > yearsMap.lastKey()) {
yearsMap.remove(yearsMap.lastKey());
yearsMap.put(currentYear, value);
}
}
public Map<String, String> getValuesForYear(int year) {
return yearsMap.get(year);
}
@Override
public String toString() {
return String.valueOf(yearsMap);
}
}
简单用法:
ObjectMapper om = new ObjectMapper();
Pojo demoClass = om.readValue(json, Pojo.class);
System.out.println(demoClass);
System.out.println("Year 2014: " + demoClass.getValuesForYear(2014));
以上程序打印:
{2014={1=1, 2=1, 3=1}, 2013={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2012={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2011={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2010={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2009={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}}
Year 2014: {1=1, 2=1, 3=1}