import org.json.JSONObject; public class JsonExample { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "John"); obj.put("age", 25); obj.put("isMarried", true); String jsonString = obj.toString(); System.out.println(jsonString); } }
import org.json.JSONObject; public class JsonExample { public static void main(String[] args) { String jsonString = "{\"name\":\"John\", \"age\":25, \"isMarried\":true}"; JSONObject obj = new JSONObject(jsonString); String jsonString2 = obj.toString(); System.out.println(jsonString2); } }This example creates a JSON-formatted string and uses it to create a new JSONObject. The toString() method is then called on this object to obtain the JSON-formatted string representation of this object. The package library for this example is org.json.