0

I'm trying to get the values from array in a JSON file. Array only has a square brackets and not curly brackets something like this: "{"name_of_the_array: ["value_1", "value_2"]"}"

I found here some answer but it did not work for me the code of the answer is:

JSONArray array = new JSONArray("array_name");

        for (int i = 0; i < array.length(); i++) {

            str = array.get(i).toString();
            list.add(str);
        }

the list is ArrayList list.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

{} denotes a JSONObject so you have to create a JSONObject first. [] denotes a JSONArray. You have to fetch the JSONArray from JSONObject with key.

Use this code

    JSONObject jsonObject = new JSONObject(jsonString);
    JSONArray array = jsonObject.getJSONArray("array_name");
    for (int i = 0; i < array.length(); i++) {

        str = array.get(i).toString();
        list.add(str);
    }

Check this and this for parsing

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
0
try something like this
   JSONArray array = new JSONArray("array_name");

    for (int i = 0; i < array.length(); i++) {

        str = array.getJSONObject(i).getString("string_name");
        list.add(str);
    }

  or you can try this too
  JSONArray array = new JSONArray("array_name");

    for (int i = 0; i < array.length(); i++) {

        str = array.getString("string_name");
        list.add(str);
    }
primo
  • 1,340
  • 3
  • 12
  • 40