简体   繁体   English

需要有关如何将具有相同键的两个值从 json 格式解析为 java map 的建议

[英]need suggestions about how to parse two values with same key from json format into java map

I'm working on reading ldif attributes from LDAP into my java program, my original thought is to put those attributes in to a map, then parse them from it, but I found that, there is an attributes call "departmentNumber", which contains two json format values,我正在将 LDAP 中的 ldif 属性读取到我的 java 程序中,我最初的想法是将这些属性放入 map 中,然后从那里解析它们,其中包含我调用的“departmentNumber”属性,两个 json 格式值,

departmentNumber: {"sid":"729999","uid":501,"name":"tebase","role":"managers","title":"sales","groups":["others"]}
departmentNumber: {"sid":"724605","uid":37,"name":"tebase","role":"managers","title":"develope","groups":["leaders"]}

so I used map to put them, some codes shown below,所以我用 map 来放它们,一些代码如下所示,

but my way can only retrieve the 1st json value, which is the one that contains {"sid":"729999"}, but no 2nd value that contains {"sid":"724605"},但我的方法只能检索第一个 json 值,即包含 {"sid":"729999"} 的值,但没有包含 {"sid":"724605"} 的第二个值,

I observed the program log is:我观察到程序日志是:

this is the log that read from ldif attributes这是从 ldif 属性读取的日志

2019 22:30:58,237 authentication.mzauth   authentication.mzauth.doAuthenticate(mzauth.java:44)  
{"uid":"brucelee","mail":"brucelee@gm.macom","displayName":"Bruce Lee","givenName":"Bruce Lee","departmentNumber":"{\"sid\":\"729999\",\"uid\":501,\"name\":\"teabas\",\"role\":\"managers\",\"title\":\"sales\",\"groups\":[\"others\"]}","objectClass":"inetOrgPerson","description":"Bruce Lee","sn":"Bruce Lee","cn":"Z39414","department":"000000","info":"2006/02/12"}   

this is the log that put into a map and parse them out这是放入 map 并解析出来的日志

2019 22:30:58,247 services.LdapService services.LdapService.getUser(LdapService.java:251)
ldap user:{"username":"brucelee","fullname":"Bruce Lee","email":"brucelee@gm.macom","departmentid":"729999","titles":[{"schoolid":"729999","titles":["managers","sales"]}],"cloudroles":{"usage":"clouddev","roles":[{"appname":"mail","departmentid":"729999","titles":["managers","sales"]}]},"guid":"c00d5ba660145307c84f2c1e1c557e4ededaf1830029d40aa5b244027","pid":"Z39414","openid":"http://openid.macom/brucelee"}  

as you can read, there is only 1 value, the one which is sid:72999, instead of two values,如您所见,只有 1 个值,即 sid:72999,而不是两个值,

the original ldif attributes is:原来的 ldif 属性是:

LDAPv3 base with scope subtree filter: uid=brucelee requesting: ALL带有 scope 子树过滤器的 LDAPv3 基础:uid=brucelee 请求:ALL

Z39414, Managers, developer.alle.com dn: cn=Z39414,ou=Managers,dc=developer,dc=alle,dc=com uid: brucelee userPassword: qwdmsdierf mail: brucelee@gm.macom info: 2006/02/12 sn: Bruce Lee departmentNumber: {"sid":"729999","uid":501,"name":"tebase","role":"managers","title":"sales","groups":["others"]} departmentNumber: {"sid":"724605","uid":37,"name":"tebase","role":"managers","title":"develope","groups":["leaders"]} department: 000000 givenName: Bruce Lee objectClass: inetOrgPerson objectClass: organizationalPerson objectClass: person objectClass: top cn: Z39414 displayName: Bruce Lee description: Bruce Lee Z39414, Managers, developer.alle.com dn: cn=Z39414,ou=Managers,dc=developer,dc=alle,dc=com uid: brucelee userPassword: qwdmsdierf mail: brucelee@gm.macom info: 2006/02/12 sn:李小龙部门编号:{"sid":"729999","uid":501,"name":"tebase","role":"managers","title":"sales","groups":[ "其他"]} 部门编号:{"sid":"724605","uid":37,"name":"tebase","role":"managers","title":"develope","groups": [“leaders”]} 部门:000000 givenName:李小龙 objectClass:inetOrgPerson objectClass:organizationalPerson objectClass:person objectClass:top cn:Z39414 displayName:李小龙描述:李小龙

search result search: 2 result: 0 Success numResponses: 2 numEntries: 1搜索结果 search: 2 result: 0 Success numResponses: 2 numEntries: 1

my question is, how can I put both 2 json values into my map, or my usage of map is wrong, and there is a better way to deal with this kind of situation?我的问题是,如何将两个 json 值放入我的 map,或者我对 map 的使用是错误的,有更好的方法来处理这种情况?

public DataModel getUser(String uid, String passwd, String role, DataModel   user) throws Exception {
private Map<String, String> map = null;
   if (role.equals("managers")) {
            map = Ldap.getAttrubites(ldapurl, uid, passwd);
            List<Titles> titlesArray = new ArrayList<>();
            List<Roles> rolesArray = new ArrayList();
            map.forEach((key, value)
                    -> {logger.info("{} - {}", key, value); });
     if (key.equals("departmentNumber")) {
                List<StringModel> model = map.get("departmentNumber")).get();
                model.stream().forEach(k -> {
                for (int i = 0; i < model.size(); i++) {
                            Titles titles = new Titles();
                            List<String> titleList = new ArrayList<>();
                            String userid = model.get(i).getUid();
                            String mtitle = model.get(i).getRole();
                            String subtitle = model.get(i).getTitle();
                            titleList.add(mtitle);
                            titleList.add(subtitle);
                            titles.setSchoolid(schoolid);
                            titles.setTitles(titleList);
                            titlesArray.add(titles);

                 ........ });}
                 user.setTitles(titlesArray);
                 .........;
    } return user;
    }

The java.util.Map<K,V> interface holds only one value for each key. java.util.Map<K,V>接口为每个键只保存一个值。 If you want to hold multiple values for each key, you need to specify a collection class to the V part.如果要为每个键保存多个值,则需要为V部分指定一个集合 class。 For example, like the following.例如,像下面这样。

Map<String, List<String>> map = ...

The javax.ws.rs.core.MultivaluedMap interface defined in Java EE API is an example that achieves what you want to do. Java EE API 中定义的javax.ws.rs.core.MultivaluedMap接口是实现您想要做的事情的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM