将JSONArray转换为String数组

时间:2014-02-11 10:14:20

标签: android json arrays

可能是一个愚蠢的问题。我正在以

的形式获得JSONArray
[{'route':'route1'}, {'route':'route2'}, {'route':'route3'}]

我想把它变成一个

的String数组
["route1", "route2", "route3"]

如何?

6 个答案:

答案 0 :(得分:6)

我想到的解决方案是迭代并获取值

String[] stringArray = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
    stringArray[i]= jsonArray.getJSONObject(i).getString("route");
}

答案 1 :(得分:2)

试试这个

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

答案 2 :(得分:0)

紧跟此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"));
}

或使用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

答案 3 :(得分:0)

您可以尝试以下解决方案,

只需按照代码

替换{和}即可
String jsonString = jsonArray.toString();
jsonString.replace("},{", " ,");
String[]array = jsonString.split(" ");

或者

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

这将转换为Arraylist然后如果你想要它的字符串然后将其转换为StringArray。有关更多参考,请使用此link

答案 4 :(得分:0)

尽可能直接

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[] );

答案 5 :(得分: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;
    }

}