Json字符串取值
1、案例
String str2="{\"roles\":[],\"attributes\":[{\"values\":[\"XXXl001#xxxx\"," + "\"XXXxl002#xxxx\",\"XXXl003#xxxx\", "kMWSWEFJAjsjajJJKLjLJionGTUYugUhiuMw..HLIhIHoihIHihUIGIFuyfIUFu.\"}}";
//第二种方式
Map mapTypes = JSON.parseObject(str2);
/* for (Object obj : mapTypes.keySet()){
System.out.println("key为:"+obj+"值为:"+mapTypes.get(obj));
} */
// System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
for (Object obj : mapTypes.keySet()){
if(!"".equals(mapTypes.get("attributes"))) {
List object = (List) mapTypes.get("attributes");
/* String str3 = JSON.toJSONString(object);
List<Map<String, String>> list = jsonToList(str3);
if(list!=null){
for (int i = 0; i < list.size(); i++) {
Map<String, String> map = list.get(i);
for (Object objs : map.keySet()){
// System.out.println("key为:"+obj+"值为:"+map.get(objs));
}
}
}
*/
for (int i = 0; i < object.size(); i++) {
String str3 = JSON.toJSONString(object.get(i));
// System.out.print("++"+str3);
JSONObject jsonObject = JSONObject.parseObject(str3);
// System.out.println(jsonObject.getString("values"));
String str4 = jsonObject.getString("values");
String str5 = str4.substring(2,str4.length()-2);
String str6 = str5.replaceAll("\",\"", "");
System.out.print(str6);
}
}
}