Search code examples
jsonalfrescoxstream

Parse a JSON to object JAVA without Root


The response of my service ALFRESCO REST is:

[
{
"role": "SiteManager",
"authority":
{
    "authorityType": "USER",
    "fullName": "admin",
    "userName": "admin",
    "firstName": "Administrator",
    "lastName": "",
    "url": "\/alfresco\/service\/api\/people\/admin"
},
"url": "\/alfresco\/service\/api\/sites\/test3\/memberships\/admin"
}
,
{
"role": "SiteConsumer",
"authority":
{
    "authorityType": "GROUP",
    "shortName": "jamalgg",
    "fullName": "GROUP_jamalgg",
    "displayName": "jamalgg",
    "url": "\/alfresco\/service\/api\/groups\/jamalgg"
},
"url": "\/alfresco\/service\/api\/sites\/test3\/memberships\/GROUP_jamalgg"
}
,
{
"role": "SiteManager",
"authority":
{
    "authorityType": "GROUP",
    "shortName": "ALFRESCO_ADMINISTRATORS",
    "fullName": "GROUP_ALFRESCO_ADMINISTRATORS",
    "displayName": "ALFRESCO_ADMINISTRATORS",
    "url": "\/alfresco\/service\/api\/groups\/ALFRESCO_ADMINISTRATORS"
},
"url": "\/alfresco\/service\/api\/sites\/test3\/memberships\/GROUP_ALFRESCO_ADMINISTRATORS"
}
]

And I want to parse to list of object:

List<Memberships > listMemberships;

public class Memberships {
private String role;
private List<Authority> listAuthority ;
private String url;
}
public class Authority {
private String  authorityType;
private String  shortName;
private String  fullName;
private String  displayName;
private String  url;
}

I think that there are two solutions:

  1. how to add the tag Memberships to JSON result for encapsulates the whole.
  2. how to parse JSON result directly to my list

Thanks


Solution

  • As answered in a-better-java-json-library I would use the google-gson library.