0

I am trying to convert an array of objects into an array of strings inside a for loop.

I am extracting a property from the object the title inside the loop I have several title strings, now I want to pass that into a new array?

JSONArray shots = response.getJSONArray("shots");
for (int i=0; i < shots.length(); i++) {

    // play with data
    JSONObject post = shots.getJSONObject(i);
    String title = post.getString("title");

    // turn title into an array of strings??

}

EDIT:

I tried this

String[] mStrings = new String[15];

JSONArray shots = response.getJSONArray("shots");
for (int i=0; i < shots.length(); i++) {

    // play with data
    JSONObject post = shots.getJSONObject(i);
    String title = post.getString("title");
    //Log.d("this is my array", "arr: " + title);

    mStrings[i] = title;

}

Log.d("this is my array", "arr: " + mStrings);

the result of Log.d was D/this is my array﹕ arr: [Ljava.lang.String;@4294e620

2
  • It looks like you've done it. You've stored each title as an element of a String array. What am I not understanding here? Commented Jun 18, 2015 at 22:41
  • It failed to show me in the log basically. So I was unsure Commented Jun 18, 2015 at 23:05

4 Answers 4

1

If I understand your question correctly: you want to get an array with all the titles from the JSON shots ?

JSONArray shots = response.getJSONArray("shots");
String titles[] = new String[shots.length()];
for (int i=0; i < shots.length(); i++) {
    JSONObject post = shots.getJSONObject(i);
    String title = post.getString("title");

    titles[i] = title;
}

Using a stream you could write:

import java.util.stream.IntStream;

JSONArray shots = response.getJSONArray("shots");
String titles[] = IntStream
    .range(0, shots.length())
    .mapToObj(i -> shots.getJSONObject(i))
    .map(post -> post.getString("title"))
    .toArray(String[]::new);
}

If you are using this JSONArray, you can even write:

JSONArray shots = response.getJSONArray("shots");
String titles[] = shots.stream()
    .map(post -> ((JsonObject) post).getString("title"))
    .toArray(String[]::new);
Sign up to request clarification or add additional context in comments.

Comments

1

The best way to accomplish this is to create a new ArrayList that you can store each String into as you parse it from the JSONObject.

ArrayList<String> stringList = new ArrayList<String>();

JSONArray shots = response.getJSONArray("shots");
for (int i=0; i < shots.length(); i++) {

    JSONObject post = shots.getJSONObject(i);
    String title = post.getString("title");

    stringList.add(title);

}

3 Comments

Yeah but what if I want to access the array outside the loop?
Can I just call stringList? ultimately I want to pass it into an adapter
Yes, because stringList is instantiated outside of the loop you will be able to use it in a larger scope. Once you have finished parsing through each JSONObject you will have filled the the list with strings and can simply pass the stringList object to your adapter.
1

Do you mean create an entry in a String array for each title?

If so:

JSONArray shots = response.getJSONArray("shots");

List<String> titles = new ArrayList<String>();

for (int i=0; i < shots.length(); i++) {

    // play with data
    JSONObject post = shots.getJSONObject(i);
    String title = post.getString("title");
    titles.add(title)

    // turn title into an array of strings??

}

String[] titleArr = new String[titles.size()];
titleArr = titles.toArray(titleArr);

If that's not what you're looking for, please provide some more detail in the question.

1 Comment

Okay I am trying to create an array to pass into an Adapter so I can have a listView
1

Use

Log.d("this is my array", "arr: " + Arrays.toString(mStrings));

instread of

Log.d("this is my array", "arr: " + mStrings);

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.