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"]}