[{
"roleModel": {
"roleId": 27,
"roleCode": "role_analyst",
"roleName": "分析师"
},
"workspaceName": "营销组",
"workspaceId": "asgaeqwesd-123asdqw"
}, {
"roleModel": {
"roleId": 27,
"roleCode": "role_workspace_analyst",
"roleName": "分析师"
},
"workspaceName": "管理组",
"workspaceId": "aaaase123-1"
}]
具体方法
1.转列表对象
导入依赖与包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.0.M2</version>
</dependency>
import cn.hutool.json.JSONUtil;
String s ="[{
"roleModel": {
"roleId": 27,
"roleCode": "role_analyst",
"roleName": "分析师"
},
"workspaceName": "营销组",
"workspaceId": "asgaeqwesd-123asdqw"
}, {
"roleModel": {
"roleId": 27,
"roleCode": "role_workspace_analyst",
"roleName": "分析师"
},
"workspaceName": "管理组",
"workspaceId": "aaaase123-1"
}]"
List<Object> list = JSONUtil.parseArray(s);
for (Object o : list) {System.out.println(o);}
最后输出的数据;
{
"roleModel": {
"roleId": 27,
"roleCode": "role_analyst",
"roleName": "分析师"
},
"workspaceName": "营销组",
"workspaceId": "asgaeqwesd-123asdqw"
}
{
"roleModel": {
"roleId": 27,
"roleCode": "role_workspace_analyst",
"roleName": "分析师"
},
"workspaceName": "管理组",
"workspaceId": "aaaase123-1"
}
2.遍历取值;object获取属性值
List<Object> list = JSONUtil.parseArray(s);
List workSpaceIds=new ArrayList<>();
for (Object o : list) {
Map entity = (Map)o;
workSpaceIds.add(entity.get("workspaceId").toString());
}
for(int i=0;i<workSpaceIds.size();i++){
System.out.println(workSpaceIds.get(i).toString());
}
以上即为本人项目中的处理思路,若有帮助到你,那真的太好了!文章来源:https://www.toymoban.com/news/detail-555835.html
若没有望亲喷文章来源地址https://www.toymoban.com/news/detail-555835.html
到了这里,关于Java 列表json格式的字符串转列表对象 并遍历取值的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!