0

I am trying to parse a JSON Array which looks something like this

"data":["data1","data2","data3"]

If I write JSONArray arr = obj1.getJSONArray("data");, this will provide me with the JSON array but since the key name from key-value pair is missing, how will I retrieve "data1", "data2" and "data3"?

2 Answers2

1

JSON arrays allows for non json children. In this case, the children are of String value:

for(int i=0;i<arr.length();i++) {
    String value = arr.getString(i);
}

My syntax might be inaccurate

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

Assuming this is your JSON,

{"data":["data1","data2","data3"]}

Use the following to retrieve the array,

JSONArray arrJson = jsonData.getJSONArray("data");     
String[] arr = new String[arrJson.length()];     
for(int i = 0; i < arrJson.length(); i++) {    
    arr[i] = arrJson.getString(i);     
}