JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "John"); System.out.println(jsonObject.toString()); // Output: {"name":"John"}
JSONObject jsonObject = new JSONObject("{\"name\":\"John\",\"age\":26}"); System.out.println(jsonObject.toString()); // Output: {"name":"John","age":26} jsonObject.put("age", 27); System.out.println(jsonObject.toString()); // Output: {"name":"John","age":27}In this example, we create a new JSONObject from a string and print it. We then use the "put" method to update the value of the "age" key to 27. We print the JSON object again to confirm the update. Overall, the net.sf.json package library provides powerful and flexible utilities for working with JSON data in Java, including the useful "put" method for updating JSON objects.