转换为ArrayList并加载到Spinner?

时间:2013-09-10 11:32:48

标签: android json

如何将以下内容转换为ArrayList?因为我需要将它加载到android中的Spinner。

json string:{"3":"APM","2":"YAZ","1":"SST","5":"IDB","4":"NCRC"}

3 个答案:

答案 0 :(得分:1)

试试这种方式

private ArrayList<String> parseJsonData(String strJson) {

        ArrayList<String> items = new ArrayList<String>();
        try{
            JSONObject data = new JSONObject(strJson);
            if (data != null) {
                Iterator<String> it = data.keys();
                while (it.hasNext()) {
                    String key = it.next();

                    try {
                        items.add(data.getString(key));
                    } catch (Throwable e) {
                        e.printStackTrace();

                    }
                }
            }
        }catch (Exception e) {
        }
        return items;
    }

答案 1 :(得分:1)

上课:

  class MyArray {
     public  ArrayList<Details > result = new ArrayList<Details>();
   class Details {
     String id;
     String value;
 }
 }

然后在你的课堂上:

      String json = {"3":"APM","2":"YAZ","1":"SST","5":"IDB","4":"NCRC"} ;
      MyClass obj = new Gson().fromJson(json,MyClass.class);

      String strID =obj.result.get(0).id ;  // value of 1st id
      String strVal = obj.result.get(0).value  // value of 1st value

      String strID2 =obj.result.get(0).id ;  // value of 2nd id
      String strVal2 = obj.result.get(0).value  // value of 2nd value

答案 2 :(得分:0)

使用JSONObject作为解析字符串。

示例:

List<String> list = new ArrayList<String>();
JSONObject json = new JSONObject(yourString);
list.add(json.getString("1"));