如何将以下内容转换为 ArrayList ?因为我需要将它加载到 android 中的 Spinner。
json字符串:{"3":"APM","2":"YAZ","1":"SST","5":"IDB","4":"NCRC"}
试试这个方法
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;
}
上课:
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
使用JSONObject解析字符串。
例子:
List<String> list = new ArrayList<String>();
JSONObject json = new JSONObject(yourString);
list.add(json.getString("1"));