skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, October 15, 2012

Creating JSON data in Java

Posted by Raju Gupta at 4:00 AM – 0 comments
 
JSON (JavaScript Object Notation) is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects). The JSON format is often used for transmitting structured data over a network connection in a process called serialization. Its main application is in AJAX web application programming, where it serves as an alternative to the traditional use of the XML format.

//Place json-rpc-1.0.jar  file in classpath.



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 
import net.sf.json.JSONObject; //Import file. 

public class JsonMain {   

 public static void main(String[] args) {    
  Map map = new HashMap();  
  map.put("A", 10);        
  map.put("B", 20);    
  map.put("C", 30);   
  JSONObject json = new JSONObject();
  json.accumulateAll(map);
  System.out.println(json.toString());
  List list = new ArrayList(); 
  list.add("Sunday");
  list.add("Monday");
  list.add("Tuesday");
  json.accumulate("weekdays", list);
  System.out.println(json.toString()); 
 }
}




//Output
{"A":10,"B":20,"C":30}
{"A":10,"B":20,"C":30,"weekdays":["Sunday","Monday","Tuesday"]} 
Labels: Java JSON Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger