java - Take an array of objects and turn into array of strings? -
i trying convert array of objects array of strings inside loop.
i extracting property object title
inside loop have several title
strings, want pass new array?
jsonarray shots = response.getjsonarray("shots"); (int i=0; < shots.length(); i++) { // play data jsonobject post = shots.getjsonobject(i); string title = post.getstring("title"); // turn title array of strings?? }
edit:
i tried this
string[] mstrings = new string[15]; jsonarray shots = response.getjsonarray("shots"); (int i=0; < shots.length(); i++) { // play data jsonobject post = shots.getjsonobject(i); string title = post.getstring("title"); //log.d("this array", "arr: " + title); mstrings[i] = title; } log.d("this array", "arr: " + mstrings);
the result of log.d
d/this array﹕ arr: [ljava.lang.string;@4294e620
if understand question correctly: want array titles json shots ?
jsonarray shots = response.getjsonarray("shots"); string titles[] = new string[shots.length()]; (int i=0; < shots.length(); i++) { jsonobject post = shots.getjsonobject(i); string title = post.getstring("title"); titles[i] = title; }
using stream 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 using jsonarray
, can write:
jsonarray shots = response.getjsonarray("shots"); string titles[] = shots.stream() .map(post -> ((jsonobject) post).getstring("title")) .toarray(string[]::new);
Comments
Post a Comment