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.

Probably a stupid question. I'm getting a JSONArray in the form of

[{'route':'route1'}, {'route':'route2'}, {'route':'route3'}]

I want to get it into a String array of

["route1", "route2", "route3"]

How?

share|improve this question

closed as unclear what you're asking by Selvin, hichris123, Scott Barta, halfelf, ling.s Feb 12 at 4:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

7 Answers

up vote 0 down vote accepted

The solution that comes to my mind would be to iterate through and just grab the values

String[] stringArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
    stringArray[i]= jsonArray.getJSONObject(i).getString("route");
}
share|improve this answer
add comment

There already was that problem on Stack. Here is the solution. Look here or here. There is what You need.

share|improve this answer
    
Sorry for the duplicate. Thanks –  Thahzan Mohomed Feb 11 at 10:27
    
You're welcome! Mark/rate the answer please. –  RobertoB Feb 11 at 10:28
add comment

Try this

JSONArray jsonArray = null;
    try {
        jsonArray = new JSONArray(responseString);

        if (jsonArray != null) {

            String[] strArray = new String[jsonArray.length()];

            for (int i = 0; i < jsonArray.length(); i++) {
                strArray[i] = jsonArray.getJSONObject(i).getString("route");
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
share|improve this answer
add comment

Followed by this TUTORIAL

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(i = 0; i < arr.length; i++){
    list.add(arr.getJSONObject(i).getString("name"));
}

Or use GSON

Gson gson = new Gson();
    Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);

//(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]

//(Deserialization)
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
//ints2 is same as ints
share|improve this answer
add comment

You can try below solutions,

Just replace { and } by following code

String jsonString = jsonArray.toString();
jsonString.replace("},{", " ,");
String[]array = jsonString.split(" ");

Or

JSONArray arr = new JSONArray(yourJSONresponse);
List<String> list = new ArrayList<String>();
for(i = 0; i < arr.length; i++){
    list.add(arr.getJSONObject(i).getString("name"));
}

this will convert to Arraylist and then if you want it to string then convert it to StringArray. for more reference use this link

share|improve this answer
add comment

as straight forward as it can be

List<String> list = new ArrayList<String>();
for( int ix = 0; ix < yourArray.length(); ix++ ){
  list.add( yourArray.getJSONObject( ix ).getString( "route" ) );
}
return list.toArray( new String[] );
share|improve this answer
add comment
// try this way here i gave with demo code
public class MyActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try{
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("route","route1");
            jsonArray.put(jsonObject1);
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("route","route2");
            jsonArray.put(jsonObject2);
            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.put("route","route3");
            jsonArray.put(jsonObject3);

            String[] array = jsonArrayToArray(jsonArray);

            for (int i=0;i<array.length;i++){
                Log.i((i+1)+" Route : ",array[i]);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public String jsonToStrings(JSONObject object) throws JSONException {
        String data ="";
        Iterator keys = object.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            data+=fromJson(object.get(key)).toString()+",";
        }
        return data;
    }


    private Object fromJson(Object json) throws JSONException {
        if (json == JSONObject.NULL) {
            return null;
        } else if (json instanceof JSONObject) {
            return jsonToStrings((JSONObject) json);
        } else if (json instanceof JSONArray) {
            return jsonArrayToArray((JSONArray) json);
        } else {
            return json;
        }
    }

    private String[] jsonArrayToArray(JSONArray array) throws JSONException {
        ArrayList<Object> list = new ArrayList<Object>();
        int size = array.length();
        for (int i = 0; i < size; i++) {
            list.add(fromJson(array.get(i)));
        }
        ArrayList<String> arrayList = new ArrayList<String>();
        for (int i=0;i<list.size();i++){
            String[] row = ((String)((String)list.get(i)).subSequence(0,((String)list.get(i)).length()-1)).split(",");
            for (int j=0;j<row.length;j++){
                arrayList.add(row[j]);
            }
        }
        String[] strings = new String[arrayList.size()];
        for (int k=0;k<strings.length;k++){
            strings[k]=arrayList.get(k);
        }

        return strings;
    }

}
share|improve this answer
add comment

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