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

1/12/2016 How to use json object in java | Tutorialspoint examples

Tutorialspoint examples
(http://tutorialspointexamples.com)
Learn java tutorial with examples for beginners online

Search

How To Use JSON Object In Java?
Let us discuss how to use JSON object in java with the help of below example.

Steps:
1. Include JSON jar in classpath.
2. Create JSONObject object.
3. Add data to JSONObject.
3. Process the object.

Example:
JSONTest.java
package com.javawithease.business; 
import net.sf.json.JSONObject; 
/** 
 * This class show use JSON object in java. 
 * @author javawithease 
 */ 
public class JSONTest { 
  public static void main(String args[]){ 
      //Create JSON object 
      JSONObject obj = new JSONObject(); 
  
      //Add data to JSON object. 
      obj.put("name", "Bharat"); 
      obj.put("rollNo", "MCA/07/05"); 
      obj.put("id", new Integer(50));       
  
      //Print JSON object. 
      System.out.print(obj); 
  } 
}

Output:

http://tutorialspointexamples.com/json­object­in­java/ 1/5
1/12/2016 How to use json object in java | Tutorialspoint examples

{"name":"Bharat","rollNo":"MCA/07/05","id":50}

Download this example. (http://tutorialspointexamples.com/wp­
content/uploads/2015/06/JSONExample1.rar)
 
Next Topic: How to parse json in java with example? (http://tutorialspointexamples.com/how­to­
parse­json­in­java­with­example/)
Previous Topic: How to access json object array in javascript?
(http://tutorialspointexamples.com/how­to­access­json­object­array­in­javascript/)

Related Topics:
How to build java project using ant in eclipse? (http://tutorialspointexamples.com/how­to­build­java­
project­using­ant­in­eclipse/)
JAXB marshalling – convert java object to xml example. (http://tutorialspointexamples.com/jaxb­
marshalling­convert­java­object­to­xml­example­using­one­pojo/)
How to create pdf file in java using iText jar? (http://tutorialspointexamples.com/how­to­create­pdf­file­
in­java­using­itext­jar/)
Generics class example. (http://tutorialspointexamples.com/generics­class­example/)
OGNL in struts 2. (http://tutorialspointexamples.com/ognl­in­struts­2/)
Hibernate One­to­One Mapping using xml. (http://tutorialspointexamples.com/hibernate­one­to­one­
mapping­using­xml/)
Send inline image in email using JavaMail API. (http://tutorialspointexamples.com/send­inline­image­
in­email­using­javamail­api/)
Quartz 2 JobListener example. (http://tutorialspointexamples.com/quartz­2­joblistener­example/)
 

  94    0   Share 1   Share 0    0   Share 0  


  0    0   Share 0    92

JSON Tutorial

JSON overview. (http://tutorialspointexamples.com/json­overview/) 
JSON format and data types (http://tutorialspointexamples.com/json­format­and­data­types/) 
JSON object from string. (http://tutorialspointexamples.com/how­to­create­json­object­from­string­in­
javascript/) 
Access json object array. (http://tutorialspointexamples.com/how­to­access­json­object­array­in­
javascript/) 
JSON object in java. (http://tutorialspointexamples.com/how­to­use­json­object­in­java/) 
Parse json in java. (http://tutorialspointexamples.com/how­to­parse­json­in­java­with­example/) 
Download JSON jar. (http://tutorialspointexamples.com/wp­content/uploads/2015/06/JSONLib.rar)

http://tutorialspointexamples.com/json­object­in­java/ 2/5

You might also like