1.JSON字符串
String content = "[{" +
" \"key\": \"UserRole\"," +
" \"name\": \"员工角色\"" +
"}, {" +
" \"key\": \"RolePermission\"," +
" \"name\": \"角色权限\"" +
"}]";
2.映射实体
public class PermissionContent implements Serializable {
private static final long serialVersionUID = 8891095994295070691L;
private String key;
private String name;
}
3.使用ObjectMapper
ObjectMapper mapper = new ObjectMapper();
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, PermissionContent.class);
List<PermissionContent> userList = mapper.readValue(content, listType);
System.out.println(userList.get(0).getKey());
本文介绍了一种使用Java处理JSON字符串的方法,并演示了如何将JSON字符串映射到自定义实体类的过程。通过实例展示了创建JSON字符串、定义实体类及使用ObjectMapper进行解析的具体步骤。
751

被折叠的 条评论
为什么被折叠?



