- org.json Tutorial
- org.json - Home
- org.json - Overview
- org.json - Environment Setup
- CSV Examples
- org.json - CDL
- Cookie Examples
- org.json - Cookie
- org.json - CookieList
- HTTP Header Examples
- org.json - HTTP
- JSON Examples
- org.json - JSONArray
- org.json - JSONML
- org.json - JSONObject
- org.json - JSONStringer
- Property Examples
- org.json - Property
- XML Examples
- org.json - XML
- Exception Handling
- org.json - JSONException Handling
- org.json Useful Resources
- org.json - Quick Guide
- org.json - Useful Resources
- org.json - Discussion
org.json - JSONObject
JSONObject class is a unordered collection of key-value pairs. It provides methods to access values by key and to put values. Following types are supported −
Boolean
JSONArray
JSONObject
Number
String
JSONObject.NULL object
Example
import org.json.JSONArray; import org.json.JSONObject; public class JSONDemo { public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); jsonObject.put("Name", "Robert"); jsonObject.put("ID", 1); jsonObject.put("Fees", new Double(1000.21)); jsonObject.put("Active", new Boolean(true)); jsonObject.put("Other Details", JSONObject.NULL); JSONArray list = new JSONArray(); list.put("foo"); list.put(new Integer(100)); jsonObject.put("list",list); System.out.println(jsonObject); } }
Output
{"Active":true,"Other Details":null,"ID":1,"Fees":1000.21,"list":["foo",100],"Name":"Robert"}
Advertisements