【问题标题】:Convert JSONArray into a String Array [closed]将 JSONArray 转换为字符串数组 [关闭]
【发布时间】:2014-03-09 01:33:03
【问题描述】:

可能是一个愚蠢的问题。我得到一个

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

我想把它放到一个字符串数组中

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

怎么做?

【问题讨论】:

    标签: android json arrays


    【解决方案1】:
    // 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;
        }
    
    }
    

    【讨论】:

      【解决方案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();
          }
      

      【讨论】:

        【解决方案3】:

        我想到的解决方案是遍历并获取值

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

        【讨论】:

        • 非常感谢@nyx。你的回答对我帮助很大。
        【解决方案4】:

        尽可能直截了当

        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】:

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

          只需将 { 和 } 替换为以下代码

          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

          【讨论】:

            【解决方案6】:

            后跟这个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
            

            【讨论】:

              猜你喜欢
              • 2013-03-30
              • 2013-05-08
              • 1970-01-01
              • 2021-05-24
              • 2015-02-16
              • 1970-01-01
              • 1970-01-01
              • 2013-09-17
              • 1970-01-01
              相关资源
              最近更新 更多