Reputation: 13
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 ..
Upvotes: 0
Views: 6319
Reputation: 13
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 ..
Upvotes: 1
Reputation: 1018
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.
Upvotes: 0
Reputation: 766
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();
}
}
Upvotes: 0
Reputation: 325
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
Upvotes: 0