Reputation: 579
i have this input:
[
"1",
"2",
"3",
"5",
"6",
"9",
"10"
]
Could anyone tell me how to parse this kind of json file, that I get from the web? It doesn't have any key for the attribute or something like that.
Upvotes: 0
Views: 375
Reputation: 16798
You are having JSONArray as your response string so you need to create JSONArray for that
JSONArray jsonArray = new JSONArray(responseString);
Now, you can retrieve the values by their index positions.like,
for(int i = 0; i < jsonArray.length(); i++){
String value = jsonArray.getString(i);
}
Complete code to parse this JSON is,
JSONArray jsonArray = new JSONArray(responseString);
for(int i = 0; i < jsonArray.length(); i++){
String value = jsonArray.getString(i);
}
Upvotes: 2
Reputation: 8587
With org.json package, like this (assuming it is in a string):
JSONArray myJSONArray = new JSONArray(inputString);
You can then get the individual elements using:
myJSONArray.getString(i);
Upvotes: 2
Reputation: 24853
Try this..
JSONArray array = new JSONArray(response); // get the response as JSONArray
for(int i=0; i < array.length(); i++)
{
Log.v("ALL--", array.getString(i)); // get the values as string using for loop
}
Upvotes: 2