This is the result I am getting from a webservice
"year":["2014","2013","2012","2011","2010","2009","2008","2007","2006","2005","2004","2003","2002","2001","2000","1999","1998","1997","1996","1995","1994","1993","1992","1991","1990","1989","1988","1987","1986","1985","1984","1983","1982","1981","1980","1979","1978","1977","1976","1975","1974","1973","1972","1971","1970","1969","1968","1967","1966","1965","1964","1963","1962","1961","1960","1959","1958","1957","1956","1955","1954","1953","1952","1951","1950","1949","1948","1941","1940","1938","1933","1928"]
I would likes to loop this in an activity and show the listing. So I wrote the following
JSONArray ar=obj.getJSONArray("year");
for(i=0;i<ar.length();i++){
// How to get the value of the item in array
// Tried ar[i]
}
My doubt is How to get the value of the item in array ? I need a listing like this 2014,2013,2012....
Please give me an idea Thanks
asked Feb 18, 2014 at 6:16
ramesh
4,09213 gold badges76 silver badges119 bronze badges
2 Answers 2
Try this..
You can get like ar.getString(i)
JSONArray ar=obj.getJSONArray("year");
for(i=0;i<ar.length();i++){
Log.v("Result--",""+ar.getString(i));
}
answered Feb 18, 2014 at 6:17
Hariharan
24.9k6 gold badges52 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Shreya Rawal
I am following the same way...but it gives the last element of array..@Hariharan
Use JSONArray.optInt to get value from JSONArray as:
JSONArray ar=obj.getJSONArray("year");
for(i=0;i<ar.length();i++){
// How to get the value of the item in array
int year=ar.optInt(i);
}
answered Feb 18, 2014 at 6:19
ρяσѕρєя K
133k54 gold badges203 silver badges216 bronze badges
Comments
default