Java JSON Tutorials

json logo

JSON (JavaScript Object Notation) is a simple, lightweight and easy to read and write data exchange formats. In Java, normally we use Jackson or Gson to parse JSON.

1. Jackson

Tested with Jackson 2.9.8

2. Gson

Tested with Gson 2.8.5

3. FastJson

Tested with FastJson 1.2.57

FAQs

References

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
26 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
25 Comment authors
hamid mohammadiPilarneeleshSandeep Andhalejaychang Recent comment authors
newest oldest most voted
Bhagavan
Guest
Bhagavan

Nice one

jessebs
Guest
jessebs

http://javafromjson.dashingrocket.com/ can generate the Java Classes from JSON so you don’t have to write them yourself

Sujata Sid
Guest
Sujata Sid

It’s Really Helpfull Tutorial!!!!!!!!!!!

hamid mohammadi
Guest
hamid mohammadi

hi
in “Gson” there is a word that is not correct . misspell

Pilar
Guest
Pilar

Great job! Mkyong, could you help with Java Persistent APIs examples, Please.

neelesh
Guest
neelesh

Great tutorial. Thanks.

Sandeep Andhale
Guest
Sandeep Andhale

mostly where we use JSON

jaychang
Guest
jaychang

FastJSON https://github.com/alibaba/fastjson
It’s really very fast

Jesse Bowes
Guest
Jesse Bowes

There’s also a tool that can take JSON as input and generate Java classes from it (both source and compiled). It can also add Jackson bindings during generation.

http://javafromjson.dashingrocket.com

Vishal
Guest
Vishal

To develop a webservice using java to fetch the data from json array and store that data on the database…

nirmal
Guest
nirmal
Rituranjan
Guest
Rituranjan
Anuj
Guest
Anuj

This code works fine for me

@GET
	@Produces("application/xml")
	public Response getList() {
		List<Person> personArray = new ArrayList<Person>();
		Person p1 = new Person();
		p1.setName("First Person");
		p1.setId(1);
		p1.setGender("M");
		Person p2 = new Person();
		p2.setName("Second Person");
		p2.setId(2);
		p2.setGender("M");
		Person p3 = new Person();
		p3.setName("Third Person");
		p3.setId(3);
		p3.setGender("M");
		personArray.add(p1);
		personArray.add(p2);
		personArray.add(p3);
		@SuppressWarnings("rawtypes")
		GenericEntity entity = new GenericEntity<List<Person>>(personArray){};
		return Response.ok(entity).build();
	}

Where as if I change Media Type to JSON, it throws error -500.

16558 [http-8080-2] ERROR org.apache.wink.server.internal.handlers.FlushResultHandler – The system could not find a javax.ws.rs.ext.MessageBodyWriter or a DataSourceProvider class for the java.util.ArrayList type and application/json mediaType. Ensure that a javax.ws.rs.ext.MessageBodyWriter exists in the JAX-RS application for the type and media type specified.

felix
Guest
felix

if the value has a double quotation(“), which function should I use?
thanks :)

Laxman Chowdary
Guest
Laxman Chowdary

You are just posting, we followed you, become victims while developing the applications.
We got the following error.If we would have followed,JDK JSON we might not face this problem…

com.google.gson.CircularReferenceException: circular reference error
	at com.google.gson.JsonSerializationVisitor.start(JsonSerializationVisitor.java:61)
	at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109)
	at com.google.gson.JsonSerializationVisitor.getJsonElementForChild(JsonSerializationVisitor.java:147)
	at com.google.gson.JsonSerializationVisitor.addAsChildOfObject(JsonSerializationVisitor.java:127)
	at com.google.gson.JsonSerializationVisitor.visitObjectField(JsonSerializationVisitor.java:114)
	at com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:69)
	at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)
Laxman Chowdary
Guest
Laxman Chowdary

I realized that this is not your mistake, Googles product bug…
Inconvenience regretted..

Sara Ali
Guest
Sara Ali

Laxman, he is only helping you and before posting any anything you should have been sure !

jyothsana
Guest
jyothsana

Thanks Mkyong… This site is really very useful for all the queries related to JSON..

Shubhrajyoti satpathy
Guest
Shubhrajyoti satpathy

ITS RIGHT THIS WEBSITE GOOD FOR JSON

sagar srinivas
Guest
sagar srinivas

Hi,

My doubt on struts2 jquery grid. I want to get the column names of a grid from struts2 Action class.
Please help me
how to get property names of Action class or DAO class.
based on the property names i generate grid dynamically.

Thank u…..

Bhaskar Reddy
Guest
Bhaskar Reddy

Really its a very good tutorial …… Thank you so much.

tttrung43
Guest
tttrung43

Thank you so much.
This tutorial is very useful with me.

trackback
Sekilas tentang Java JSON « Programming, Inspiring and more…

[…] Reference : http://www.mkyong.com/tutorials/java-json-tutorials/ […]

rojer
Guest
rojer

very nice for understanding i never see like this before.

Sylar
Guest
Sylar

Implementing an interface means overiding its methods. So @overide was used.

chandan singh
Guest
chandan singh

we can not override abstract methods we can implement abstract methods.

????
Guest
????

you are a very good engineer.

trackback
Links for August 9th through August 12th — Vinny Carpenter's blog

[…] Java JSON library tour – In this series of Java JSON tutorials, we focus on three popular third party Java libraries to process JSON data, which are Jackson, Google Gson and JSON.simple […]

trackback
Blog bookmarks 08/13/2011 « My Diigo bookmarks

[…] Java JSON Tutorial […]

Muskandaza
Guest
Muskandaza

Excellent , mkyong @ BEST . Really Appreciable .Tons of Best Wishes.

Thanks
Muskandaza