1

and a class named Entry. How can I convert the GlossEntry Object in json to an Entry Class Object in java.

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

2 Answers 2

3

As explained in the "Jackson in 5 minutes" page, which takes 5 minutes to read:

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
Entry entry = mapper.readValue(new File("entry.json"), Entry.class);

EDIT: sorry, I misunderstoof your question. If you want to read a sub-tree of the JSON into a Java object, then read the full JSON as a Tree, then get the sub-tree from the root node, and use ObjectMapper.treeToValue() to transform the sub tree to an Entry Java object. All the steps are described in the page I linked to.

Sign up to request clarification or add additional context in comments.

1 Comment

But then i have to map the whole json file to a class which I want to avoid.I just want to map GlossEntry
0

It depends on platform you are using for the converting; For example using Gson from google needs just tow lines of code, the following example shows how get campaign obj from this json

    String rsou = "{\"name\":\"name\",\"startDate\":\"01/01/2013 00:30\",\"endDate        \":\"01/03*d/2013 12:30\",\"variable\":\"\"}";

    Campaign newCampaign = new Gson().fromJson(rsou, Campaign.class);

1 Comment

The OP explicitely asked about Jackson, not GSon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.