http://baijiahao.baidu.com/s?id=1641906235824053291&wfr=spider&for=pc
json分json对象和json数组
String result = "
{"code":"0","msg":"操作成功",
"data":{"dataList":[{"REMARK2":"","MOD_FLAG":"N","DEPTCODE":"","ROWNO":"19","REMARK3":"","CD_TIME":"2019-07-18 18:54:46","REMARK1":"","CD_COUNT":"45","TABLENAME":"DN_TA_SP_APPROVE_MATERIAL","CD_BATCH":"2019071500007","ID":"258"},{"REMARK2":"","MOD_FLAG":"N","DEPTCODE":"","ROWNO":"20","REMARK3":"","CD_TIME":"2019-07-18 18:54:50","REMARK1":"","CD_COUNT":"13","TABLENAME":"DN_TA_SP_APPROVE_RESULT","CD_BATCH":"2019071500006","ID":"259"}] ,
"recordNum":"13193"}}
";
json串转json对象
JSONObject jsStr = JSONObject.fromObject(result);
json串转json数组
JSONArray jsonValue = JSONArray.fromObject(dataList);
json转bean
//当你json对象里的key名称为大小加下划线的,类里的属性名称为驼峰命名时 下列方法可用来转换
for(int i=0;i<jsonValue.size();i++){
JSONObject hostObject = jsonValue.getJSONObject(i); //循环json数组转为json对象
//对json串的key值名称处理
for(int j = 0;j<hostObject.size();j++){
String key = (String) hostObject.keys().next(); //循环获取json对象的key
String fomatKey = StringUtils.underlineToCamelCase(key);//处理key 将key名称下划线转驼峰命名
String value = hostObject.getString(key);
hostObject.put(fomatKey, value);//将处理好的 key,value put 进json对象
hostObject.discard(key);//删除原理的key value
//System.out.println("key: "+key+",value"+value+" fomatKey "+fomatKey);
}
}
/*
* 下划线转驼峰命名
* @param String
* */
public static String underlineToCamelCase(String str) {
str = str.toLowerCase();
final StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("_(\\w)");
Matcher m = p.matcher(str);
while (m.find()){
m.appendReplacement(sb,m.group(1).toUpperCase());
}
m.appendTail(sb);
return sb.toString();
}
json在线转换工具
BeanUtils.copyProperties(源object,目标object);// 源object的所有属性值 赋给 目标object ,要求属性名称一致