json array 转为 list/set 的两种方式
一,采用jackson
jackson spring mvc 默认集成
private static HashSet<String> getPermissionSet(String permsStr) {
ObjectMapper mapper = new ObjectMapper();
try {
HashSet<String> lst = (HashSet<String>) mapper.readValue(permsStr, HashSet.class);
} catch (Exception e1) {
e1.printStackTrace();
}
return perms;
}
二,采用 org.json 包
需要在 pom.xml 中导入包
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
private static HashSet<String> getPermissionSet(String permsStr) {
HashSet<String> perms = new HashSet<String>();
try {
JSONArray ja = new JSONArray(permsStr);
int len = ja.length();
for (int i = 0; i < len; i ++) {
String singlePerm = ja.getString(i);
perms.add(singlePerm);
}
} catch (JSONException e) {
e.printStackTrace();
}
return perms;
}
更多推荐
所有评论(0)