Parse json from End to Start

Christoph S

I'm currently trying to optimize a json parsing in java.
Is is somehow possible to parse a Json from end-to-start?

I use the faster-xml/jackson-core (2.2.3) library to parse kinds of following json objects These are in fact nested objects grouped by Year, and then followed by month. See sample below, there are years where only some months are relevant, so others are omited. The json Object is part of an website I am parsing in Java. Let's say I only want to parse the latest 5 entrys, staring by the current year (2014) descending. Problem: The json object is in ascending order, so to get the latest 5 entrys I have to get trough the whole json Object from start to end.

Is there a way to do it the other way round? Or to reoder the json object? Other ideas to solve this?

  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}}');

I don't want it as an object strucutre, so I am parsing it token by token.
Here is my json parsing code which works fine.

    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();
Michał Ziober

You can simply do it using @JsonAnySetter annotation. Your POJO class should look like this:

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);
    }
}

Simple usage:

ObjectMapper om = new ObjectMapper();
Pojo demoClass = om.readValue(json, Pojo.class);
System.out.println(demoClass);
System.out.println("Year 2014: " + demoClass.getValuesForYear(2014));

Above program prints:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fullcalendar start end time from json feed

From Dev

How to remove parentheses from JSON in start and end?

From Dev

Get stock quotes as JSON from Yahoo Finance with Start and End dates

From Dev

loop from end to start

From Dev

loop from end to start

From Dev

Loop from start to end to start in php

From Dev

Parse the data from JSON

From Dev

Parse from JSON response

From Dev

JSon Parse from Wiki

From Dev

Parse JSON from GoogleApiBooks

From Dev

NSJSONSerialization Fails to Parse Valid JSON - "Garbage at End"

From Dev

How to parse multiple lines with start and end tokens in Parsec

From Dev

javascript : parse float with string prefix in both start and end

From Dev

Compare start time and end time in list json

From Dev

Jumping from start to end of code block - vim

From Dev

Start and end date from date range

From Dev

Create range of years from start to end.

From Dev

Regex start searching from the end of the string (reverse)

From Dev

Start reading data in query from end (of file)

From Dev

Create a sequence from vectors of start and end numbers

From Dev

specifying start and end positions from python list

From Dev

Date generate from start to end date in java

From Dev

Start and end date from date range

From Dev

Distinguish ID from start & end datetime

From Dev

Extract bits from start to end in javascript

From Dev

specifying start and end positions from python list

From Dev

How to get accurate time from start to end

From Dev

Java read .log file from end to start

From Dev

JAVA: Addition from start to end using recursion