Parse Json in Java Example.

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1/12/2016

Parsejsoninjavaexample|Tutorialspointexamples

Tutorialspointexamples
(http://tutorialspointexamples.com)
Learnjavatutorialwithexamplesforbeginnersonline
Search

HowToParseJsonInJavaWithExample?
LetusdiscusshowtoparseJSONobjectsusingJavawiththehelpofbelowexample.

Steps:
1.IncludeJSONjarinclasspath.
2.DefineJSONstring.
3.CreateJSONparserobject
4.ParseJSONstringusingJSONparser.
5.Processtheobject.

Example:
JSONTest.java

http://tutorialspointexamples.com/parsejsoninjava/

1/6

1/12/2016

Parsejsoninjavaexample|Tutorialspointexamples

packagecom.javawithease.business;

importorg.json.simple.JSONArray;
importorg.json.simple.JSONObject;
importorg.json.simple.parser.JSONParser;
importorg.json.simple.parser.ParseException;

/**
*ThisclassisusedtoparsetheJSONstring.
*@authorjavawithease
*/
publicclassJSONTest{
publicstaticvoidmain(Stringargs[]){

//JSONString

StringjsonString="[{\"name\":\"Swati\",\"rollNo\":"+

"\"MCA/07/01\",\"id\":10},{\"name\":\"Prabhjot\",\""+

"rollNo\":\"MCA/07/39\",\"id\":50}]";

//CreateJSONparserobject.

JSONParserparser=newJSONParser();

try{

//ParseJSONstringusingJSONparser.

Objectobject=parser.parse(jsonString);

JSONArrayarray=(JSONArray)object;

System.out.println("Firstobject:");

System.out.println(array.get(0));

//GetJSONobjectfromJSONarray.

JSONObjectjsonObject=(JSONObject)array.get(1);

System.out.println("Secondobject:");

System.out.println("Name:"+jsonObject.get("name"));

}catch(ParseExceptione){

e.printStackTrace();

}
}
}

Output:
Firstobject:
{"id":10,"rollNo":"MCA\/07\/01","name":"Swati"}
Secondobject:
Name:Prabhjot

Downloadthisexample.(http://tutorialspointexamples.com/wp
content/uploads/2015/06/JSONExample2.rar)

PreviousTopic:HowtouseJSONobjectinjava?(http://tutorialspointexamples.com/howtouse
jsonobjectinjava/)

RelatedTopics:
Howtobuildjavaprojectusingantineclipse?(http://tutorialspointexamples.com/howtobuildjava

http://tutorialspointexamples.com/parsejsoninjava/

2/6

1/12/2016

Parsejsoninjavaexample|Tutorialspointexamples

projectusingantineclipse/)
JAXBmarshallingconvertjavaobjecttoxmlexample.(http://tutorialspointexamples.com/jaxb
marshallingconvertjavaobjecttoxmlexampleusingonepojo/)
HowtocreatepdffileinjavausingiTextjar?(http://tutorialspointexamples.com/howtocreatepdffile
injavausingitextjar/)
Genericsclassexample.(http://tutorialspointexamples.com/genericsclassexample/)
OGNLinstruts2.(http://tutorialspointexamples.com/ognlinstruts2/)
HibernateOnetoOneMappingusingxml.(http://tutorialspointexamples.com/hibernateonetoone
mappingusingxml/)
SendinlineimageinemailusingJavaMailAPI.(http://tutorialspointexamples.com/sendinlineimage
inemailusingjavamailapi/)
Quartz2JobListenerexample.(http://tutorialspointexamples.com/quartz2joblistenerexample/)

252

0
0

Share
Share

42

Share

Share

141

JSON Tutorial
JSONoverview.(http://tutorialspointexamples.com/jsonoverview/)
JSONformatanddatatypes(http://tutorialspointexamples.com/jsonformatanddatatypes/)
JSONobjectfromstring.(http://tutorialspointexamples.com/howtocreatejsonobjectfromstringin
javascript/)
Accessjsonobjectarray.(http://tutorialspointexamples.com/howtoaccessjsonobjectarrayin
javascript/)
JSONobjectinjava.(http://tutorialspointexamples.com/howtousejsonobjectinjava/)
Parsejsoninjava.(http://tutorialspointexamples.com/howtoparsejsoninjavawithexample/)
DownloadJSONjar.(http://tutorialspointexamples.com/wpcontent/uploads/2015/06/JSONLib.rar)

http://tutorialspointexamples.com/parsejsoninjava/

3/6

You might also like