i have some problem Like This ..
["IMG-20181223-WA0010.jpg","IMG-20181225-WA0013.jpg","IMG-20181229-WA0001.jpg"]
How To convert JSONArray like that from real array in java ?? Sorry Bad English ..
i'm put the JSONArray to the string toString(), and i'm convert by regular expression like this ..
public String getImg(String d){
return rubahFormat(d).split(",");
}
public String rubahFormat(String d){
return d.replaceAll("[\\[\\]\\\"]","");
}
Thx ..
String[] stringArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
stringArray[i]= (String)jsonArray.getJSONObject(i);
}
jsonArray is your JSON object and stringArray variable will store as string array type
String[] which the OP doesn't have.(String)jsonArray.getJSONObject(i)import org.json.JSONArray; in your code?You should try like this and it will help you
import net.sf.json.JSONArray;
public class JsonArraytoArray {
JSONArray jsonArray = new JSONArray();
public void convertJsonarrtoArray() {
jsonArray.add("java");
jsonArray.add("test");
jsonArray.add("work");
String[] stringArray = new String[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
stringArray[i] = jsonArray.getString(i);
}
System.out.println("stringArray " + stringArray.length);
}
public static void main(String[] args) {
JsonArraytoArray d = new JsonArraytoArray();
d.convertJsonarrtoArray();
}
}
You can use Java Streams to do that for you:
String[] data = new String[] { "value1", "value2", "value3" };
String jsonArray = "[\"" + Stream.of(data).collect(Collectors.joining("\", \"")) + "\"]";
The steam collector adds the ", " between two values. Now simply add the [" at the beginning, and the corresponding end.
JSONArrayand need to convert it toString[]? is that what you mean?