In general, such a trap ... there is a line that comes from a well-known site. the problem is that in different categories it has many different levels of attachments, which it is impossible to catch with simple for and if. In this connection, I ask for help to push on the right path to get an ArrayList (map) with the data of the type name = aaa id = 111 parent = xx, etc. at the output.

{ "id": "204", "title": "Π’ΠΈΠΏ объявлСния", "type": "select", "values": [ { "id": "1074", "title": "ΠŸΡ€ΠΎΠ΄Π°ΠΌ", "params": [ { "id": "532", "title": "Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", "type": "select", "values": [ { "id": "5494", "title": "Π“Π°Ρ€Π°ΠΆ", "params": [ { "id": "783", "title": "Π’ΠΈΠΏ Π³Π°Ρ€Π°ΠΆΠ°", "multiple": true, "type": "select", "values": [ { "id": "10991", "title": "Π–Π΅Π»Π΅Π·ΠΎΠ±Π΅Ρ‚ΠΎΠ½Π½Ρ‹ΠΉ" }, { "id": "10993", "title": "ΠšΠΈΡ€ΠΏΠΈΡ‡Π½Ρ‹ΠΉ" }, { "id": "10995", "title": "ΠœΠ΅Ρ‚Π°Π»Π»ΠΈΡ‡Π΅ΡΠΊΠΈΠΉ" } ] } ] }, { "id": "5495", "title": "ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ", "params": [ { "id": "781", "title": "Π’ΠΈΠΏ ΠΌΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚Π°", "multiple": true, "type": "select", "values": [ { "id": "10983", "title": "ΠœΠ½ΠΎΠ³ΠΎΡƒΡ€ΠΎΠ²Π½Π΅Π²Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³" }, { "id": "10985", "title": "ΠŸΠΎΠ΄Π·Π΅ΠΌΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³" }, { "id": "10987", "title": "ΠšΡ€Ρ‹Ρ‚Π°Ρ стоянка" }, { "id": "10989", "title": "ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚Π°Ρ стоянка" } ] } ] } ] }, { "id": "785", "title": "ΠžΡ…Ρ€Π°Π½Π°", "type": "select", "values": [ { "id": "10997", "title": "Π”Π°" }, { "id": "10999", "title": "НСт" } ] } ] }, { "id": "1075", "title": "Π‘Π΄Π°ΠΌ", "params": [ { "id": "563", "title": "Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", "type": "select", "values": [ { "id": "5819", "title": "Π“Π°Ρ€Π°ΠΆ", "params": [ { "id": "784", "title": "Π’ΠΈΠΏ Π³Π°Ρ€Π°ΠΆΠ°", "multiple": true, "type": "select", "values": [ { "id": "10992", "title": "Π–Π΅Π»Π΅Π·ΠΎΠ±Π΅Ρ‚ΠΎΠ½Π½Ρ‹ΠΉ" }, { "id": "10994", "title": "ΠšΠΈΡ€ΠΏΠΈΡ‡Π½Ρ‹ΠΉ" }, { "id": "10996", "title": "ΠœΠ΅Ρ‚Π°Π»Π»ΠΈΡ‡Π΅ΡΠΊΠΈΠΉ" } ] } ] }, { "id": "5820", "title": "ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ", "params": [ { "id": "782", "title": "Π’ΠΈΠΏ ΠΌΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚Π°", "multiple": true, "type": "select", "values": [ { "id": "10984", "title": "ΠœΠ½ΠΎΠ³ΠΎΡƒΡ€ΠΎΠ²Π½Π΅Π²Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³" }, { "id": "10986", "title": "ΠŸΠΎΠ΄Π·Π΅ΠΌΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³" }, { "id": "10988", "title": "ΠšΡ€Ρ‹Ρ‚Π°Ρ стоянка" }, { "id": "10990", "title": "ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚Π°Ρ стоянка" } ] } ] } ] }, { "id": "786", "title": "ΠžΡ…Ρ€Π°Π½Π°", "type": "select", "values": [ { "id": "10998", "title": "Π”Π°" }, { "id": "11000", "title": "НСт" } ] } ] }, { "id": "1073", "title": "ΠšΡƒΠΏΠ»ΡŽ", "params": [ { "id": "756", "title": "Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", "type": "select", "values": [ { "id": "10897", "title": "Π“Π°Ρ€Π°ΠΆ" }, { "id": "10898", "title": "ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ" } ] } ] }, { "id": "1076", "title": "Π‘Π½ΠΈΠΌΡƒ", "params": [ { "id": "779", "title": "Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", "type": "select", "values": [ { "id": "10981", "title": "Π“Π°Ρ€Π°ΠΆ" }, { "id": "10982", "title": "ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ" } ] } ] } ] } 
  • do you need all the nodes in the json object to add to the list or some specific ones? - Artem Konovalov
  • all nodes, then to insert them into other requests. the fact is that in different categories there can be up to 10 nested nodes ... here’s how to sort out this business for a start ... - baralgin1003

2 answers 2

Connect the json library

  <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> 

An approximate recursive traverse of the json ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° will be the following:

 JSONObject jsonObject = new JSONObject(jsonBodyStr); List<Map<String, String>> result = new ArrayList<>(); parser(jsonObject, "0", result); for (Map<String, String> map : result) System.out.println(map.toString()); private static void parser(JSONObject jsonObject, String parentId, List<Map<String, String>> result) { String currentId = parentId; if (jsonObject.has("id") && jsonObject.has("title")) { Map<String, String> object = new HashMap<>(); currentId = jsonObject.getString("id"); object.put("id", currentId); object.put("name", jsonObject.getString("title")); object.put("parent", parentId); result.add(object); } for (String fieldName : jsonObject.keySet()) if (jsonObject.get(fieldName) instanceof JSONArray) { JSONArray array = jsonObject.getJSONArray(fieldName); for (int i = 0; i < array.length(); i++) parser(array.getJSONObject(i), currentId, result); } } 
  • the json library does not work with my json string .. I try to adapt to gson, but it has no analog keyset ... - baralgin1003
  • Why doesn't it work? I'm on the line in question, tested, everything worked. - Artem Konovalov
  • writes "Cannot resolve method keySet ()". I work in idea. library hooked up - baralgin1003
  • one
    you call the keySet method on the wrong type of object you want. Apparently you have a library in which there is also a JSONObject class, check the imports. - Artem Konovalov
  • one
    Imports are correct. Then I do not understand what could be the matter. ok, there are methods with similar functionality, they can pull out field names, for example, names() , keys() , getNames(Object object) . Get the name of the fields and iterate over them. - Artem Konovalov

Adapted the code of Artem Konovalov to use GSON, now everything works! Thank you very much

 private static void parser(JsonObject jsonObject, String parentId, List<Map<String, String>> result){ String currentId = parentId; if (jsonObject.has("id") && jsonObject.has("title")) { Map<String, String> object = new HashMap<>(); currentId = jsonObject.get("id").toString(); object.put("id", currentId); object.put("name", jsonObject.get("title").toString()); object.put("parent", parentId); result.add(object); } for (Map.Entry<String, JsonElement> fieldName : jsonObject.entrySet()) if (jsonObject.get(fieldName.getKey()) instanceof JsonArray) { JsonArray array = jsonObject.getAsJsonArray(fieldName.getKey()); for (int i = 0; i < array.size(); i++) parser(array.get(i).getAsJsonObject(), currentId, result); } } 

at the exit we have

 {id="204", name="Π’ΠΈΠΏ объявлСния", parent=0} {id="1074", name="ΠŸΡ€ΠΎΠ΄Π°ΠΌ", parent="204"} {id="532", name="Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", parent="1074"} {id="5494", name="Π“Π°Ρ€Π°ΠΆ", parent="532"} {id="783", name="Π’ΠΈΠΏ Π³Π°Ρ€Π°ΠΆΠ°", parent="5494"} {id="10991", name="Π–Π΅Π»Π΅Π·ΠΎΠ±Π΅Ρ‚ΠΎΠ½Π½Ρ‹ΠΉ", parent="783"} {id="10993", name="ΠšΠΈΡ€ΠΏΠΈΡ‡Π½Ρ‹ΠΉ", parent="783"} {id="10995", name="ΠœΠ΅Ρ‚Π°Π»Π»ΠΈΡ‡Π΅ΡΠΊΠΈΠΉ", parent="783"} {id="5495", name="ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ", parent="532"} {id="781", name="Π’ΠΈΠΏ ΠΌΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚Π°", parent="5495"} {id="10983", name="ΠœΠ½ΠΎΠ³ΠΎΡƒΡ€ΠΎΠ²Π½Π΅Π²Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³", parent="781"} {id="10985", name="ΠŸΠΎΠ΄Π·Π΅ΠΌΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³", parent="781"} {id="10987", name="ΠšΡ€Ρ‹Ρ‚Π°Ρ стоянка", parent="781"} {id="10989", name="ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚Π°Ρ стоянка", parent="781"} {id="785", name="ΠžΡ…Ρ€Π°Π½Π°", parent="1074"} {id="10997", name="Π”Π°", parent="785"} {id="10999", name="НСт", parent="785"} {id="1075", name="Π‘Π΄Π°ΠΌ", parent="204"} {id="563", name="Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", parent="1075"} {id="5819", name="Π“Π°Ρ€Π°ΠΆ", parent="563"} {id="784", name="Π’ΠΈΠΏ Π³Π°Ρ€Π°ΠΆΠ°", parent="5819"} {id="10992", name="Π–Π΅Π»Π΅Π·ΠΎΠ±Π΅Ρ‚ΠΎΠ½Π½Ρ‹ΠΉ", parent="784"} {id="10994", name="ΠšΠΈΡ€ΠΏΠΈΡ‡Π½Ρ‹ΠΉ", parent="784"} {id="10996", name="ΠœΠ΅Ρ‚Π°Π»Π»ΠΈΡ‡Π΅ΡΠΊΠΈΠΉ", parent="784"} {id="5820", name="ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ", parent="563"} {id="782", name="Π’ΠΈΠΏ ΠΌΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚Π°", parent="5820"} {id="10984", name="ΠœΠ½ΠΎΠ³ΠΎΡƒΡ€ΠΎΠ²Π½Π΅Π²Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³", parent="782"} {id="10986", name="ΠŸΠΎΠ΄Π·Π΅ΠΌΠ½Ρ‹ΠΉ ΠΏΠ°Ρ€ΠΊΠΈΠ½Π³", parent="782"} {id="10988", name="ΠšΡ€Ρ‹Ρ‚Π°Ρ стоянка", parent="782"} {id="10990", name="ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚Π°Ρ стоянка", parent="782"} {id="786", name="ΠžΡ…Ρ€Π°Π½Π°", parent="1075"} {id="10998", name="Π”Π°", parent="786"} {id="11000", name="НСт", parent="786"} {id="1073", name="ΠšΡƒΠΏΠ»ΡŽ", parent="204"} {id="756", name="Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", parent="1073"} {id="10897", name="Π“Π°Ρ€Π°ΠΆ", parent="756"} {id="10898", name="ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ", parent="756"} {id="1076", name="Π‘Π½ΠΈΠΌΡƒ", parent="204"} {id="779", name="Π’ΠΈΠ΄ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°", parent="1076"} {id="10981", name="Π“Π°Ρ€Π°ΠΆ", parent="779"} {id="10982", name="ΠœΠ°ΡˆΠΈΠ½ΠΎΠΌΠ΅ΡΡ‚ΠΎ", parent="779"} 

The problem of non-working code was in Android Studio, under which I ran normal java code.