Write this:
JSONObject outputJson = new JSONObject(); JSONArray list = new JSONArray(); //Делаем первый элемент id JSONObject id1 = new JSONObject(); id1.put("id", "auto"); //Делаем второй элемент id JSONObject id2 = new JSONObject(); id2.put("id", "auto2"); //Добавляем их в наш массив list.add(id1); list.add(id2); //Добавляем в главный outputJson.put("car", list);
Read this:
String json = "{\"car\":[{\"id\":\"auto\"},{\"id\":\"auto2\"}]}"; //Преобразуем в объект JSONObject inputJson = (JSONObject) JSONValue.parse(json); //Получаем список id JSONArray ids = (JSONArray) inputJson.get("car");
Well, the output elements (JSONObject) ids.get(0) , etc.
Also the conclusion can be made through the cycle:
for (int i = 0; i < ids.size(); i++) { System.out.println((JSONObject) ids.get(i)); }
The result is the following:
Входной JSON -> {"car":[{"id":"auto"},{"id":"auto2"}]} Выходные элементы: {"id":"auto"} , {"id":"auto2"}