String jsonStr = "{ \"fruits\": [\"apple\", \"banana\", \"orange\"] }"; JSONObject jsonObj = new JSONObject(jsonStr); JSONArray fruitsArr = jsonObj.getJSONArray("fruits"); System.out.println(fruitsArr.toString());
JSONObject personObj = new JSONObject(); JSONArray hobbiesArr = new JSONArray(); hobbiesArr.put("reading"); hobbiesArr.put("singing"); personObj.put("name", "John"); personObj.put("age", 35); personObj.put("hobbies", hobbiesArr); System.out.println(personObj.toString());In this example, we create a new JSONObject and an empty JSONArray. We add some data to the JSONArray and then add the array to the personObj with a key called "hobbies". We also add some other properties to the personObj like name and age. Finally, we print the personObj to the console. Package Library: org.json.