Professional Documents
Culture Documents
Tutorialspoint examples
(http://tutorialspointexamples.com)
Learn java tutorial with examples for beginners online
Search
How To Parse Json In Java With Example?
Let us discuss how to parse JSON objects using Java with the help of below example.
Steps:
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.
Example:
JSONTest.java
http://tutorialspointexamples.com/parsejsoninjava/ 1/6
1/12/2016 Parse json in java example | Tutorialspoint examples
package com.javawithease.business;
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 javawithease
*/
public class JSONTest {
public static void main(String args[]) {
// JSON String
String jsonString = "[{\"name\":\"Swati\",\"rollNo\":" +
"\"MCA/07/01\",\"id\":10},{\"name\":\"Prabhjot\",\"" +
"rollNo\":\"MCA/07/39\",\"id\":50}]";
// 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:");
System.out.println(array.get(0));
// 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) {
e.printStackTrace();
}
}
}
Output:
First object:
{"id":10,"rollNo":"MCA\/07\/01","name":"Swati"}
Second object:
Name:Prabhjot
Download this example. (http://tutorialspointexamples.com/wp
content/uploads/2015/06/JSONExample2.rar)
Previous Topic: How to use JSON object in java? (http://tutorialspointexamples.com/howtouse
jsonobjectinjava/)
Related Topics:
How to build java project using ant in eclipse? (http://tutorialspointexamples.com/howtobuildjava
http://tutorialspointexamples.com/parsejsoninjava/ 2/6
1/12/2016 Parse json in java example | Tutorialspoint examples
projectusingantineclipse/)
JAXB marshalling – convert java object to xml example. (http://tutorialspointexamples.com/jaxb
marshallingconvertjavaobjecttoxmlexampleusingonepojo/)
How to create pdf file in java using iText jar? (http://tutorialspointexamples.com/howtocreatepdffile
injavausingitextjar/)
Generics class example. (http://tutorialspointexamples.com/genericsclassexample/)
OGNL in struts 2. (http://tutorialspointexamples.com/ognlinstruts2/)
Hibernate OnetoOne Mapping using xml. (http://tutorialspointexamples.com/hibernateonetoone
mappingusingxml/)
Send inline image in email using JavaMail API. (http://tutorialspointexamples.com/sendinlineimage
inemailusingjavamailapi/)
Quartz 2 JobListener example. (http://tutorialspointexamples.com/quartz2joblistenerexample/)
JSON Tutorial
JSON overview. (http://tutorialspointexamples.com/jsonoverview/)
JSON format and data types (http://tutorialspointexamples.com/jsonformatanddatatypes/)
JSON object from string. (http://tutorialspointexamples.com/howtocreatejsonobjectfromstringin
javascript/)
Access json object array. (http://tutorialspointexamples.com/howtoaccessjsonobjectarrayin
javascript/)
JSON object in java. (http://tutorialspointexamples.com/howtousejsonobjectinjava/)
Parse json in java. (http://tutorialspointexamples.com/howtoparsejsoninjavawithexample/)
Download JSON jar. (http://tutorialspointexamples.com/wpcontent/uploads/2015/06/JSONLib.rar)
http://tutorialspointexamples.com/parsejsoninjava/ 3/6