java newbees
java newbees

Reputation: 31

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,

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,

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"},

I observed the program log is:

this is the log that read from ldif attributes

2019 22:30:58,237 authentication.mzauth   authentication.mzauth.doAuthenticate(mzauth.java:44)  
{"uid":"brucelee","mail":"[email protected]","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

2019 22:30:58,247 services.LdapService services.LdapService.getUser(LdapService.java:251)
ldap user:{"username":"brucelee","fullname":"Bruce Lee","email":"[email protected]","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,

the original ldif attributes is:

LDAPv3 base with scope subtree filter: uid=brucelee requesting: ALL

Z39414, Managers, developer.alle.com dn: cn=Z39414,ou=Managers,dc=developer,dc=alle,dc=com uid: brucelee userPassword: qwdmsdierf mail: [email protected] 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

search result 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?

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;
    }

Upvotes: 0

Views: 74

Answers (1)

Takahiko Kawasaki
Takahiko Kawasaki

Reputation: 19011

The java.util.Map<K,V> interface holds only one value for each key. If you want to hold multiple values for each key, you need to specify a collection class to the V part. 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.

Upvotes: 0

Related Questions