Reputation: 1553
I'm parsing an url using json. I'm getting the value of each tag and storing it in a string using getString
in a for
-loop.
What I want is to store the String
value into a String
array. I'm a noob as far as android development is concerned.
Below is the code:
JSONObject json = JsonFunctions.getJSONfromURL("http://www.skytel.mobi/stepheniphone/iphone/stephenFlickr.json");
try {
JSONArray boombaby=json.getJSONArray("items");
for(int i=0;i<boombaby.length();i++) {
JSONObject e = boombaby.getJSONObject(i);
mTitleName=e.getString("title");
String mTitleImage=e.getString("image");
}
}
Upvotes: 0
Views: 204
Reputation: 18568
List<String> titles = new ArrayList<String>();
List<String> images = new ArrayList<String>();
for (int i = 0; i < boombaby.length(); i++) {
JSONObject e = boombaby.getJSONObject(i);
titles.add(e.getString("title"));
images.add(e.getString("image"));
}
and then convert list to array:
String[] titleArray = (String[])titles.toArray(new titles[titles.size()]);
String[] imageArray = (String[])images.toArray(new titles[images.size()]);
Upvotes: 0
Reputation: 162
My solution:
String[] convert2StringArr(String str)
{
if(str!=null&&str.length()>0)
{
String[] arr=new String[str.length()];
for(int i=0;i<str.length();i++)
{
arr[i]=new String(str.charAt(i)+"");
}
return arr;
}
return null;
}
Upvotes: 1
Reputation: 691755
Use a List to store your titles, and another to store your images. Or design a class holding two fields (title and image), and store instances of this class in a List:
List<String> titles = new ArrayList<String>();
List<String> images = new ArrayList<String>();
for (int i = 0; i < boombaby.length(); i++) {
JSONObject e = boombaby.getJSONObject(i);
titles.add(e.getString("title"));
images.add(e.getString("image"));
}
Read the Java tutorial about collections. This is a must-know.
Upvotes: 3