Org.Json - CSV Examples
Org.Json - Cookie Examples
Org.Json - HTTP Header Examples
Org.Json - JSON Examples
Org.Json - Property Examples
Org.Json - XML Examples
Org.Json - Exception Handling
Org.Json - Useful Resources
Org.Json - JSONObject Class
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 - JSONObject of Strings
JsonDemo.java
package com.tutorialspoint; import org.json.JSONObject; public class JsonDemo { public static void main(String[] args) { JSONObject week = new JSONObject(); week.put("Mon", "Monday"); week.put("Tue", "Tuesday"); week.put("Wed", "Wednesday"); week.put("Thu", "Thursday"); week.put("Fri", "Friday"); System.out.println(week); } }
Output
{"Thu":"Thursday","Tue":"Tuesday","Wed":"Wednesday","Fri":"Friday","Mon":"Monday"}
Example - JSONObject of Integers
JsonDemo.java
package com.tutorialspoint; import org.json.JSONObject; public class JsonDemo { public static void main(String[] args) { JSONObject weeklyScores = new JSONObject(); weeklyScores.put("Mon", 100); weeklyScores.put("Tue", 101); weeklyScores.put("Wed", 102); weeklyScores.put("Thu", 103); weeklyScores.put("Fri", 104); System.out.println(weeklyScores); } }
Output
{"Thu":103,"Tue":101,"Wed":102,"Fri":104,"Mon":100}
Example - JSONObject of Mixed Types
JsonDemo.java
package com.tutorialspoint; 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", Double.valueOf(1000.21)); jsonObject.put("Active", 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