Headline

Processing JSON with the Jackson DataBinding API using a JsonTree representation

Characteristics

This contribution process a company saved in a JSON-file to JsonTree representation. It performs the Features Total , Cut ,Parsing and Unparsing, to demonstrate how to use a JsonTree from Jackson.

Illustration

This contribution uses a ObjectMapper for parsing and unparsing. The mapper provides methods to easily map between most kinds of Java object and a JSON file. In this case the mapper maps the JSON to a JsonTree.

public static JsonNode parseFromFile(File file) {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = null;
    try {
        root = mapper.readTree(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return root;
}
A JsonTree is build of JsonNodes, which can be simple key-value pairs, JSON objects or JSON arrays.

A part from the total method as a simple example to demonstrate how to search for certain values:

    if (node.get("salary") != null)
        return node.get("salary").asDouble();
    else {
        if (node.has("departments")) {
            Iterator<JsonNode> iterator = node.path("departments").elements();
            while (iterator.hasNext()) {
                result += total(iterator.next());
            }
        }
        if (node.has("employees")) {
            Iterator<JsonNode> iterator = node.path("employees").elements();
            while (iterator.hasNext()) {
                result += total(iterator.next());
            }
        }
    }
The class Unparsing used a ObjectWriter to prettyprint the tree to its JSON representation.
    ObjectMapper mapper = new ObjectMapper();
    PrettyPrinter printer = new DefaultPrettyPrinter();
    ObjectWriter writer = mapper.writer(printer);

Usage

Use Gradle to build this contribution. For running and editing this contribution supports Eclipse.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.