How to parse json in java with example?

Let us discuss how to parse JSON objects using Java with the help of below example.


1. Include JSON jar in classpath.
2. Define JSON string.
3. Create JSON parser object
4. Parse JSON string using JSON parser.
5. Process the object.


import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 * This class is used to parse the JSON string.
 * @author w3spoint
public class JSONTest {
    public static void main(String args[]) {
	  // JSON String
	  String jsonString = "[{\"name\":\"Swati\",\"rollNo\":" +
		"\"MCA/07/01\",\"id\":10},{\"name\":\"Prabhjot\",\"" +
	  // Create JSON parser object.
	  JSONParser parser = new JSONParser();
	  try {
		// Parse JSON string using JSON parser.
		Object object = parser.parse(jsonString);
		JSONArray array = (JSONArray) object;
		System.out.println("First object:");
		// Get JSON object from JSON array.
		JSONObject jsonObject = (JSONObject) array.get(1);
		System.out.println("Second object:");
		System.out.println("Name:" + jsonObject.get("name"));
	  } catch (ParseException e) {


First object:
Second object:

Download this example.
Previous Topic: How to use JSON object in java?


Please Share