标签:end 字符串数组 pen length token main -- new 字符串
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
google ---gson字符串数组用GSON解析然后用逗号隔开拼接,去掉最后一个逗号
标签:end 字符串数组 pen length token main -- new 字符串
原文地址:http://www.cnblogs.com/flywang/p/7989609.html