0

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");
  }
}
1
  • Right now you are not even storing you are creating new string every time and throwing the other one somewhere in memory until GC picks it up :) Commented Nov 23, 2011 at 7:52

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

i stored it in arrayllist and it works fine..but what i want is to store it in string array..is it any way possible...
How about using HashMap<String,String> title can be key and value image! :)
open javadoc and read what different method a ArrayList has, you can convert into array easily by images.toArray();
@kingston: an ArrayList offers more more than a simple array. If you have a List, there's (most of the time) no reason to transform it into an array. If you really want to, just use String[] array = list.toArray(new String[list.size()]); You could have found this yourself by just reading the javadoc: docs.oracle.com/javase/6/docs/api/java/util/List.html
the thing is i m doing lazy loading of images..i m reengineering a previously built code where a function call is of type String array..i tried to pass the function call as arraylist and this complicates the entire module and there comes a need to change the variables and function..it would be much simpler if i pass it in string array .is there any way to convert the array list into string...any help will be much appreciated
|
1

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;
}

1 Comment

This converts a string to a string array of its characters. This won't help the OP.
0
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()]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.