CodesJava

Easy learning with example program codes

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.codesjava.business;
import net.sf.json.JSONObject;
/**
 * This class show use JSON object in java.
 * @author codesjava
 */
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:

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

Download this example.
 
Next Topic: How to parse json in java with example?
Previous Topic: How to access json object array in javascript?

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki