List<Map<String, Object>> 转String
时间: 2023-12-17 16:26:16 浏览: 85
list转String
5星 · 资源好评率100%
将List<Map<String, Object>>转换为String有多种方法,以下是两种常见的方法:
方法一:使用数组
可以通过遍历List<Map<String, Object>>并将其转换为两个数组,然后使用Arrays.toString()方法将数组转换为字符串。示例代码如下:
```
List<Map<String, Object>> activityStaList = activityService.getStatusByDeptId(deptId);
String[] actNameArr = new String[activityStaList.size()];
String[] staArr = new String[activityStaList.size()];
for(int i = 0; i < activityStaList.size(); i++){
actNameArr[i] = (String)activityStaList.get(i).get("activityName");
staArr[i] = (String)activityStaList.get(i).get("activityStatus");
}
String actNames = Arrays.toString(actNameArr);
String actStatuss = Arrays.toString(staArr);
```
方法二:使用StringBuilder
可以使用StringBuilder将List<Map<String, Object>>转换为字符串。示例代码如下:
```
List<Map<String, Object>> activityStaList = activityService.getStatusByDeptId(deptId);
StringBuilder actNamesBuilder = new StringBuilder();
StringBuilder actStatussBuilder = new StringBuilder();
for(int i = 0; i < activityStaList.size(); i++){
String actName = String.valueOf(activityStaList.get(i).get("activityName"));
String actStatus = String.valueOf(activityStaList.get(i).get("activityStatus"));
if(i != (activityStaList.size()-1)){
actNamesBuilder.append(actName).append(",");
actStatussBuilder.append(actStatus).append(",");
}else{
actNamesBuilder.append(actName);
actStatussBuilder.append(actStatus);
}
}
String actNames = actNamesBuilder.toString();
String actStatuss = actStatussBuilder.toString();
```
阅读全文