Ibm mobilefirst 在customAttributes中返回JSON数组

Ibm mobilefirst 在customAttributes中返回JSON数组,ibm-mobilefirst,worklight-security,Ibm Mobilefirst,Worklight Security,从my开始,我可以在custom attributes部分返回一个组列表,但是我想知道我需要做什么才能在std JSON结构中返回它们 如果我发回Java列表 HashMap<String, Object> customAttributes = new HashMap<String, Object>(); customAttributes.put("AuthenticationDate", new Date()); List<String> groups =

从my开始,我可以在custom attributes部分返回一个组列表,但是我想知道我需要做什么才能在std JSON结构中返回它们

如果我发回Java列表

HashMap<String, Object> customAttributes = new HashMap<String, Object>();
customAttributes.put("AuthenticationDate", new Date());
List<String> groups = new ArrayList<String>();
groups.add("Users");
groups.add("Managers");
customAttributes.put("Groups", groups);
UserIdentity identity = new UserIdentity(loginModule, USERNAME, "Fred Flintstone", null, customAttributes, PASSWORD);
如果我在hashMap中添加组

List<Map<String, Object>> groups = new ArrayList<Map<String, Object>>();
HashMap<String, Object> groupMap1 = new HashMap<String, Object>();
groupMap1.put("id", "Users");
groups.add(groupMap1);
HashMap<String, Object> groupMap2 = new HashMap<String, Object>();
groupMap2.put("id", "Managers");
groups.add(groupMap2);
customAttributes.put("Groups", groups);

UserIdentity identity = new UserIdentity(loginModule, USERNAME, "Fred Flintstone", null, customAttributes, PASSWORD);
我真正想要的是这样的东西

"attributes":{"Groups":[{"id" : "Users"}, {"id" :"Managers"}],"AuthenticationDate":"Tue Nov 26 12:13:40 EST 2013"}

为了做到这一点,您需要在将组HashMap放入属性HashMap之前将其转换为JSON对象

比如:

...
groups.add(groupMap1);
groups.add(groupMap2);
customAttributes.put("Groups", JSONObject(groups));
将HashMap转换为JSONObject的语法会因项目访问的JSON库而异。如果它没有内置方法,那么您必须手动循环HashMap,以便将其转换为正确的JSONObject

编辑:

由于groups对象是作为字符串传入的,因此可以使用JSON.parse将其转换为JSON对象

function getSecretData(){
    var user = WL.Server.getActiveUser();
    var attributes = user.attributes;
    var groups = JSON.parse(attributes.Groups);

    return {
        groups: groups
    };
}
...
groups.add(groupMap1);
groups.add(groupMap2);
customAttributes.put("Groups", JSONObject(groups));
function getSecretData(){
    var user = WL.Server.getActiveUser();
    var attributes = user.attributes;
    var groups = JSON.parse(attributes.Groups);

    return {
        groups: groups
    };
}