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 ..
4 Answers 4
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 ..
answered Mar 11, 2019 at 21:57
Arjun Nurdin
131 silver badge10 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Arjun Nurdin
thx, regex solved my problem two years ago ... btw makasih loh
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
answered Mar 11, 2019 at 1:54
user3322481
3253 silver badges4 bronze badges
3 Comments
Jason
You've sized the output array based on the input
String[] which the OP doesn't have.Arjun Nurdin
Same sir error line 3
(String)jsonArray.getJSONObject(i)user3322481
Did you add
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();
}
}
answered Mar 11, 2019 at 7:27
Ajith Deivam
7765 gold badges12 silver badges27 bronze badges
Comments
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.
answered Mar 11, 2019 at 22:10
Christoph Bimminger
1,0187 silver badges26 bronze badges
Comments
lang-java
JSONArrayand need to convert it toString[]? is that what you mean?