0

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 ..

6
  • 2
    Possible duplicate of android how to convert json array to string array Commented Mar 11, 2019 at 1:54
  • 1
    stackoverflow.com/questions/3395729/… Commented Mar 11, 2019 at 1:55
  • you mean you have JSONArray and need to convert it to String[]? is that what you mean? Commented Mar 11, 2019 at 2:39
  • Yes Bro @Deadpool .. Commented Mar 11, 2019 at 3:24
  • can you show the code that you tried and also which library are you using? @ArjunNurdin Commented Mar 11, 2019 at 3:27

4 Answers 4

1

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 ..

Sign up to request clarification or add additional context in comments.

1 Comment

thx, regex solved my problem two years ago ... btw makasih loh
0
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

3 Comments

You've sized the output array based on the input String[] which the OP doesn't have.
Same sir error line 3 (String)jsonArray.getJSONObject(i)
Did you add import org.json.JSONArray; in your code?
0

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

}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.