user997777
user997777

Reputation: 579

Json parse array with multiple string

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

Answers (3)

Jaydipsinh Zala
Jaydipsinh Zala

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

bgse
bgse

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

Hariharan
Hariharan

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

Related Questions