JSONObject obj = new JSONObject("{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}"); int length = obj.length(); System.out.println("Number of elements: " + length);
Number of elements: 3
JSONObject obj = new JSONObject("{\"employees\": [\"John\", \"Jane\", \"Bob\"]}"); int length = obj.getJSONArray("employees").length(); System.out.println("Number of employees: " + length);
Number of employees: 3In both examples, the package library used is `org.json`.