{"dingtalk_corp_role_list_response":{"result":{"has_more":"false","list":{"role_groups":[{"group_name":"默认","roles":{"roles":[{"id":278146611,"role_name":"主管理员"},{"id":278146612,"role_name":"子管理员"},{"id":278146613,"role_name":"负责人"},{"id":278146614,"role_name":"主管"}]}},{"group_name":"职务","roles":{"roles":[{"id":278146616,"role_name":"财务"},{"id":278146617,"role_name":"人事"},{"id":278146618,"role_name":"出纳"},{"id":278146619,"role_name":"销售"},{"id":278146620,"role_name":"客服"},{"id":278146621,"role_name":"质检"},{"id":278146622,"role_name":"研发"},{"id":278146623,"role_name":"行政"},{"id":278146624,"role_name":"设计"},{"id":278146625,"role_name":"产品"},{"id":278554778,"role_name":"采购"}]}},{"group_name":"岗位","roles":{"roles":[{"id":278146627,"role_name":"普通员工"},{"id":278146628,"role_name":"经理"},{"id":278146629,"role_name":"科长"},{"id":278146630,"role_name":"部长"},{"id":278146631,"role_name":"总监"},{"id":278146632,"role_name":"管理层"},{"id":278146633,"role_name":"高级管理者"},{"id":278146634,"role_name":"总经理"}]}}]}},"request_id":"7iythn3xigry"}}
调用接口返回的json如下
现在要得到的是roles的 role_name和id,具体方法如下,其实还是很简单的,只不过需要耐心点。
public static List<RoleGroup> getAllRoleList(String access_token,Long size,Long offsize) throws ApiException, OApiResultException{ DingTalkClient client = new DefaultDingTalkClient("https://eco.taobao.com/router/rest"); CorpRoleListRequest req = new CorpRoleListRequest(); List<RoleGroup> list = new ArrayList<>(); req.setSize(size);//分页大小 req.setOffset(offsize);//分页偏移 CorpRoleListResponse rsp = client.execute(req, access_token); JSONObject response = JSON.parseObject(rsp.getBody()); if (response.containsKey("dingtalk_corp_role_list_response")) { JSONObject j1 = response.getJSONObject("dingtalk_corp_role_list_response"); JSONObject j2 = j1.getJSONObject("result"); JSONObject j3 = j2.getJSONObject("list"); JSONArray j5 = j3.getJSONArray("role_groups"); for (int i = 0; i < j5.size(); i++) { JSONObject j6 = j5.getJSONObject(i); JSONObject j7 = j6.getJSONObject("roles"); JSONArray j8 = j7.getJSONArray("roles"); if(j8!=null){ for(int n = 0; n < j8.size(); n++){ RoleGroup g = j8.getObject(n, RoleGroup.class); list.add(g); }} } return list; } else { throw new OApiResultException("Role"); } }
package com.alibaba.dingtalk.openapi.demo.role; public class RoleGroup { private String id; private String role_name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getRole_name() { return role_name; } public void setRole_name(String role_name) { this.role_name = role_name; } }