public static void main(String[] args) {
String ss= "[{"type":"0","id":"123"},{"type":"0","id":"123"},{"type":"0","id":"123"},{"type":"1","id":"abc"},{"type":"1","id":"abc"}]";
Gson gson = new Gson();
StringBuffer s = new StringBuffer();
StringBuffer sb = new StringBuffer();
List<Map<String,String>> mainList = gson.fromJson(ss, new TypeToken<List<Map<String,String>>>() {
}.getType());
for(int i=0;i<mainList.size();i++){
if(mainList.get(i).get("type").equals("0")){
s.append(mainList.get(i).get("id")+",");
}
if(mainList.get(i).get("type").equals("1")){
sb.append(mainList.get(i).get("id")+",");
}
}
if (s.length() > 0)
s.deleteCharAt(s.length() - 1);
if (sb.length() > 0)
sb.deleteCharAt(sb.length() - 1);
System.out.println(s);
System.out.println(sb);
}
运行结果:
123,123,123
abc,abc