Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to ask a question about converting a jsonArray to a StringArray on android. Here is my code to get jsonArray from server.

try {
    DefaultHttpClient defaultClient = new DefaultHttpClient();
    HttpGet httpGetRequest = new HttpGet("http://server/android/listdir.php");
    HttpResponse httpResponse = defaultClient.execute(httpGetRequest);

    BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));

    String json = reader.readLine();

    //JSONObject jsonObject = new JSONObject(json);
    JSONArray jsonArray = new JSONArray(json);
    Log.d("", json);

    //Toast.makeText(getApplicationContext(), json, Toast.LENGTH_SHORT).show();

} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

And this is the JSON.

[{"name":"IMG_20130403_140457.jpg"},{"name":"IMG_20130403_145006.jpg"},{"name":"IMG_20130403_145112.jpg"},{"name":"IMG_20130404_085559.jpg"},{"name":"IMG_20130404_113700.jpg"},{"name":"IMG_20130404_113713.jpg"},{"name":"IMG_20130404_135706.jpg"},{"name":"IMG_20130404_161501.jpg"},{"name":"IMG_20130405_082413.jpg"},{"name":"IMG_20130405_104212.jpg"},{"name":"IMG_20130405_160524.jpg"},{"name":"IMG_20130408_082456.jpg"},{"name":"test.jpg"}]

How can I convert jsonArray that I've got to StringArray so I can get StringArray like this:

array = {"IMG_20130403_140457.jpg","IMG_20130403_145006.jpg",........,"test.jpg"};

Thank you for your help... :)

share|improve this question
    
you should accept an answer if you get it. –  vinoth Apr 8 '13 at 4:40
    
Why do you want to do that and waste resources? –  Nezam Apr 8 '13 at 4:45
    
@Nezam, excuse me, what do you mean..? –  andikurnia Apr 8 '13 at 4:50
    
Why would you want to convert a JSONArray which you showed to an ArrayList or an Array.Any special use? –  Nezam Apr 8 '13 at 4:51
    
yes, in spesification list it had to be stringArray... :( –  andikurnia Apr 8 '13 at 4:58

6 Answers 6

up vote 11 down vote accepted

take a look at this tutorial also you can parse above json like

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(int i = 0; i < arr.length(); i++){
    list.add(arr.getJSONObject(i).getString("name"));
}
share|improve this answer
    
this convert to Array List first, then I can convert again to StringArray, thank to give an idea... (y) –  andikurnia Apr 8 '13 at 8:36

There you go:

String tempNames = jsonObj.names().toString();  
String[] types = tempNames.substring(1, tempNames.length()-1).split(","); //remove [ and ] , then split by ','
share|improve this answer

Here is the code :

// XXX satisfies only with this particular string format
        String s = "[{\"name\":\"IMG_20130403_140457.jpg\"},{\"name\":\"IMG_20130403_145006.jpg\"},{\"name\":\"IMG_20130403_145112.jpg\"},{\"name\":\"IMG_20130404_085559.jpg\"},{\"name\":\"IMG_20130404_113700.jpg\"},{\"name\":\"IMG_20130404_113713.jpg\"},{\"name\":\"IMG_20130404_135706.jpg\"},{\"name\":\"IMG_20130404_161501.jpg\"},{\"name\":\"IMG_20130405_082413.jpg\"},{\"name\":\"IMG_20130405_104212.jpg\"},{\"name\":\"IMG_20130405_160524.jpg\"},{\"name\":\"IMG_20130408_082456.jpg\"},{\"name\":\"test.jpg\"}]";
        s = s.replace("[", "").replace("]", "");
        s = s.substring(1, s.length() - 1);
        String[] split = s.split("[}][,][{]");
        for (String string : split) {
            System.out.println(string);
        }
share|improve this answer
    
hmm, I would like if the code is for dynamic data.. but thank you for your answer... :) –  andikurnia Apr 8 '13 at 5:00
    
@andikurnia You have to get the dynamic data in s. please accept the answer if it works for you. –  Visruth CV Apr 8 '13 at 6:25
    
I think this would fail if there were escaped brackets ([) within the JSON. –  Michael Munsey Oct 24 '14 at 21:31
    
@Michael Munsey, It will not fail if the key or value contains [, but the result will not contain [, see s = s.replace("[", "").replace("]", "");. And, I have also mentioned satisfies only with this particular string format. This code should be improved based on @andikurnia's requirement. –  Visruth CV Oct 25 '14 at 5:29
public static String[] getStringArray(JSONArray jsonArray){
    String[] stringArray = null;
    int length = jsonArray.lenght();
    if(jsonArray!=null){
        stringArray = new String[length];
        for(int i=0;i<length;i++){
            stringArray[i]= array.optString(i);
        }
    }
    return stringArray;
}
share|improve this answer
1  
Code only answers, while they may be correct, are rarely as informative as ones that also explain why. Consider adding some comments or an explanation to your post. –  indivisible Jun 5 '14 at 9:18
    
Checking if "jsonArray" is null after using it - is wrong as you might get NPE before the check itself. –  android developer Nov 29 '14 at 23:50
String jsonString = jsonArray.toString();
jsonString.replace("},{", " ,");
String[]array = jsonString.split(" ");
share|improve this answer

You can loop to create the String

List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
    list.add( jsonArray.getString(i) );
}
String[] stringArray = list.toArray(new String[list.size()]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.