Laurent Hinoul

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - John F. Woods

How to use json with java

Is it possible to use json in combination with java? The answer is yes! You can easily parse json objects to java objects and and vice-versa. To do so, you can use Google’s framework ‘gson’: http://code.google.com/p/google-gson/

Maven dependency

We make a java model class ‘Person’. That person has a first name and a last name:

Now we can create a person object and parse it to a json string with gson:

The output will be: {“firstname”:”Laurent”,”lastname”:”Hinoul”}

Now we can easily parse that string back to an object:

The output will be: Person [firstname: Laurent, lastname: Hinoul]

Leave a Reply