Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13422

Take an array of objects and turn into array of strings?

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

Upvotes: 0

Views: 114

Answers (4)

Peter Neyens
Peter Neyens

Reputation: 9820

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

Upvotes: 1

user4910279
user4910279

Reputation:

Use

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

instread of

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

Upvotes: 1

Jack BeNimble
Jack BeNimble

Reputation: 36663

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.

Upvotes: 1

Andrea Thacker
Andrea Thacker

Reputation: 3470

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

}

Upvotes: 1

Related Questions