1

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?

asked Feb 11, 2014 at 10:14
0

6 Answers 6

6

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");
}
answered Feb 11, 2014 at 10:22
Sign up to request clarification or add additional context in comments.

Comments

2

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();
 }
answered Feb 11, 2014 at 10:27

Comments

0

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
answered Feb 11, 2014 at 10:17

Comments

0

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

answered Feb 11, 2014 at 10:19

Comments

0

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[] );
answered Feb 11, 2014 at 10:22

Comments

0
// 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;
 }
}
answered Feb 11, 2014 at 10:49

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.