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.

 

Posted in JSON   


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