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

 

Please follow and like us:
Content Protection by DMCA.com