9

I have an app where I fetch data from server(json) in the form of array & by using the index i used in my app, like below.

JSONObject topobj = new JSONObject(page);
JSONObject innerobj = topobj.getJSONObject("restarutant");
JSONArray phone = innerobj.getJSONArray("phone");
textViewPhone.setText("Phone: " + phone.get(0).toString() + " ,"
                    + phone.get(1).toString());

for small size array I can get like this. But when array contains 'n' no of elements and dynamically i have to use this, at that time it required to convert into String Array. Can anybody tell me how I convert the json array to String array ? Thank you

ManjotSingh
  • 713
  • 7
  • 20
Jyosna
  • 4,436
  • 13
  • 60
  • 93
  • 1
    you should accept correct answers to your questions if you've found them to be useful(See there is a tick there)and also use upvotes. It will help you get more answers. – Rishabh Jun 15 '11 at 11:41
  • possible duplicate of [Convert Json Array to normal Java Array](http://stackoverflow.com/questions/3395729/convert-json-array-to-normal-java-array) – Suma Jun 05 '13 at 11:36
  • ArrayList is not a String[] , title wont fulfill your explanation of question AND the accepted answer it for ArrayList , NOT String[] ; – Milad Apr 03 '15 at 22:22

6 Answers6

22

This should help you.

Edit:

Maybe this is what you need:

ArrayList<String> stringArray = new ArrayList<String>();
JSONArray jsonArray = new JSONArray();
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
    try {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        stringArray.add(jsonObject.toString());
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}
Community
  • 1
  • 1
Pasha
  • 2,407
  • 18
  • 25
  • Hiii,my question is different. Its for coverting objecty to string then string to json array. But I asked to convert json array to string array – Jyosna Jun 15 '11 at 10:39
  • 5
    The title is about String[] , but the accepted answer gives a ArrayList , Which are totally different however – Milad Apr 03 '15 at 22:18
6

Assume that you already have JSONArray jsonArray:

String[] stringArray = new stringArray[jsonArray.length()];
for(int i = 0, count = jsonArray.length(); i< count; i++)
{
    try {
        String jsonString = jsonArray.getString(i);
        stringArray[i] = jsonString.toString();
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55
5

This I think is what you searching for

ArrayList<String> list = new ArrayList<String>();     
JSONArray jsonArray = (JSONArray)jsonObject; 
if (jsonArray != null) { 
   for (int i=0;i<jsonArray.length();i++){ 
    list.add(jsonArray.get(i).toString()); 
} 
Andy
  • 1,815
  • 2
  • 22
  • 49
Rishabh
  • 3,752
  • 4
  • 47
  • 74
3

I just did this yesterday! If you're willing to use a 3rd party library then you can use Google GSON, with the additional benefit of having more concise code.

String json = jsonArray.toString();
Type collectionType = new TypeToken<Collection<String>>(){}.getType();
Collection<String> strings = gson.fromJson(json, collectionType);

for (String element : strings)
{
    Log.d("TAG", "I'm doing stuff with: " + element);
}

You can find more examples in the user guide.

Asim Ihsan
  • 1,501
  • 8
  • 18
0

Another elegant kotlin way:

val list = jsonArray.map { jsonElement -> jsonElement.toString() }

And just convert to array if needed

sandrstar
  • 12,503
  • 8
  • 58
  • 65
0

If you are using org.json package, Here is the kotlin way of converting json array to string array.

// get json array
val jsonArray = json.getJSONArray("field")
// convert to string array
val stringArray = Array(jsonArray.length()) { jsonArray.getString(it) }
UdaraWanasinghe
  • 2,622
  • 2
  • 21
  • 27