Jackson to convert Java Object to/from Json String

The tutorial will guide you how to use Jackson for converting Java object to Json and vice-versa.

Related Articles:
Gson for converting Java Object to/from Json
How to convert Java Object into/from XML – with Castor & Spring Boot
How to use @JsonView to serialize/de-serialize and customize JSON format from Java Object.

A. Introduce

Jackson is common library beside GSON that is used for serialize or map Java objects to JSON and vice versa.
The tutorial uses 2 methods for converting:
writeValueAsString(): Convert Java object to String as Json format
readValue(): Convert String with JSON format to Java object

B. Practice

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

II. Overview
1. Project Structure

jackson project structure

2. Step to do

– Create Maven Project with Spring Toot Suite
– Add Jackson dependency
– Create Java model class
– Use Jackson functions for converting Java Object to Json and vice-versa
– Run & Check result

III. Practice
1. Create Maven Project with Spring Toot Suite

Open Spring Tool Suite, on main menu, choose File->New->Maven Project, check Create a simple project (skip archetype selection), press Next. Then input project info:

input json-jackson project info

Press Finish, Maven project is created successfully.

2. Add Jackson dependency

3. Create Java model class

– Create a Customer model class:

4. Use Jackson functions for converting Java Object to Json and vice-versa

– Convert Java Object to String as Json format, use writeValueAsString:

– Convert String as Json Format to Java Object, use: readValue

5. Run & Check result

– Full Application class:

– Run & Result:

IV. Source code

json-jackson sourcecode

By grokonez | December 8, 2016.

Last updated on June 24, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *