Json字符串取值
Posted 向上丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json字符串取值相关的知识,希望对你有一定的参考价值。
1、案例
String str2="{"roles":[],"attributes":[{"values":["XXXl001#oa_group"," + ""XXXxl002#stockOA","XXXl003#Wifi_Group","XXXl004#PBC_Group"," + ""XXXl005#MDM_Group","XXXl006#BA_Group","XXXl007#Investment_Group"," + ""XXXl008#Finace_Group","XXXl009#Edoc_Group","XXXl010#GS_Group"," + ""XXX1211@qq.com#coremail_group","XXXl011#BWFP_Group"," + ""XXXl012#1031","XXXl013#ImagingSys_group"," + ""XXXl014@qq.con#coremail_group","XXXl015#BPS_Group"," + ""XXXl016#SAP_Group","XXXl017#MDM_SGDC","XXXl018#SGBA_Group"," + ""XXXl019#MYSOFT_SGSJT","XXXl020#HR_Group","XXXl021#ZC_Group"," + ""XXXl022#ECM_Group","XXXl023#CI_Group"],"name":"spRoleList"}]," + ""token":{"tokenId":"EOJSEFIOJASEJFJSAEFxjijiajsefjasl" + "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);
}
}
}
以上是关于Json字符串取值的主要内容,如果未能解决你的问题,请参考以下文章