There is such a JSON :

 [ { "id":1080505, "name":"Tove Lo", "genres":[ "pop", "dance", "electronics" ], "tracks":81, "albums":22, "link":somelink, "description":description, "cover":{ "small":somestring, "big":somestring1 } }, { ... } ] 
  • How can I parse it?
  • How can I get JSONArray if there is no JSONArray name? Those. after the "[" immediately come JSON elements.
  • How can it be processed? Subsequently, the data with JSON must be scored in the ListView.

    3 answers 3

      try { JSONArray rootJSON = new JSONArray(new JSONTokener(YourJson)); for (int i = 0; i < rootJSON.length(); i++) { JSONObject o = rootJSON.getJSONObject(i); Log.d("TAG", o.getString("name")); } } catch (JSONException e) { e.printStackTrace(); } 
    • Tell me please, and if I add to json in the format that I threw at the very beginning, when processing it with StringBuilder, I will add {"Artists": i.e. I'll give the name of the JSON array. Will it be considered a crutch too? - lounah

    Option using the GSON library

    1. Create model objects:

       public class Cover { @SerializedName("small") public String small; @SerializedName("big") public String big; } public class Example { @SerializedName("id") public int id; @SerializedName("name") public String name; @SerializedName("genres") public List<String> genres = new ArrayList<String>(); @SerializedName("tracks") public int tracks; @SerializedName("albums") public int albums; @SerializedName("link") public String link; @SerializedName("description") public String description; @SerializedName("cover") public Cover cover; } 
    2. Using the Gson parse input object:

       Type listType = new TypeToken<ArrayList<Example>>() {}.getType(); List<Example> yourClassList = new Gson().fromJson(jsonArray, listType); 

      The fact that in curly brackets is a JsonObject , that in a square JsonArray . In your example, JsonArray is the json root itself and the genres field.

      Look at the examples from the documentation , everything is very clear and understandable.

      • Those. json, having such a structure, like the one that I showed - this is the JSONArray, consisting of JSONObject? And inside each JSONObject there is another JSONArray, right? - lounah
      • something like that :) - gil9red