java解析任意层的json数据(递归解析) 原
wuweixiang
发布
于
2018-08-14 11:53:37
发布
于
2018-08-14 11:53:37
/**
* JSONObject解析方法(可以解析任意层json,采用递归解析的方法)
* @param objJson
* @param menu 父菜单实体类
* @param list List<Menu>集合
* @return
@SuppressWarnings("rawtypes")
public static List<Menu> analysisJson(Object objJson,Menu menu,List<Menu> list) {
// 如果obj为json数组
if (objJson instanceof JSONArray) {
//将接收到的对象转换为JSONArray数组
JSONArray objArray = (JSONArray) objJson;
//对JSONArray数组进行循环遍历
for (int i = 0; i < objArray.size(); i++) {
//新建menu对象
Menu menu1 = new Menu();
//设置menu属性
menu1.setParentIds(((JSONObject)objArray.get(i)).get("parentIds").toString());
menu1.setSort(Integer.parseInt(((JSONObject)objArray.get(i)).get("sort").toString()));
menu1.setRemarks(((JSONObject)objArray.get(i)).get("remarks").toString());
menu1.setId(((JSONObject)objArray.get(i)).get("id").toString());
menu1.setPermission(((JSONObject)objArray.get(i)).get("key").toString());
menu1.setHref(((JSONObject)objArray.get(i)).get("parameter").toString());
menu1.setName(((JSONObject)objArray.get(i)).get("name").toString());
menu1.setIsShow(((JSONObject)objArray.get(i)).get("isShow").toString());
//如果menu的ID为空,则说明父级菜单为顶级菜单,则设其ID为1
if("".equals(menu.getId())|| menu.getId() == null){
menu.setId("1");
//设置menu对象的Parent的值为父级菜单对象
menu1.setParent(menu);
//将该级菜单对象存进list集合中
list.add(menu1);
//调用回调方法
analysisJson(objArray.get(i),menu1,list);
// 如果为json对象
}else if (objJson instanceof JSONObject) {
//将Object对象转换为JSONObject对象
JSONObject jsonObject = (JSONObject) objJson;
//迭代多有的Key值
Iterator it = jsonObject.keys();
//遍历每个Key值
while (it.hasNext()) {
//将key值转换为字符串
String key = it.next().toString();
//根据key获取对象
Object object = jsonObject.get(key);
// 如果得到的是数组
if (object instanceof JSONArray) {
//将Object对象转换为JSONObject对象
JSONArray objArray = (JSONArray) object;
//调用回调方法
analysisJson(objArray,menu,list);
// 如果key中是一个json对象
else if (object instanceof JSONObject) {
//调用回调方法
analysisJson((JSONObject) object,menu,list);
return list;
} <span><span class="comment">
</span></span><pre name="code" class="java">组装实体类:
* 菜单Entity
* @author ThinkGem
* @version 2013-05-15
public class Menu extends DataEntity<Menu> {
private static final long serialVersionUID = 1L;
private Menu parent; // 父级菜单
private String parentIds; // 所有父级编号
private String name; // 名称
private String href; // 链接
private String target; // 目标( mainFrame、_blank、_self、_parent、_top)
private String icon; // 图标
private Integer sort; // 排序
private String isShow; // 是否在菜单中显示(1:显示;0:不显示)
private String permission; // 权限标识
private String userId;
public Menu(){
super();
this.sort = 30;
this.isShow = "1";
public Menu(String id){
super(id);
@JsonBackReference
@NotNull
public Menu getParent() {
return parent;
public void setParent(Menu parent) {
this.parent = parent;
@Length(min=1, max=2000)
public String getParentIds() {
return parentIds;
public void setParentIds(String parentIds) {
this.parentIds = parentIds;
@Length(min=1, max=100)
public String getName() {
return name;
public void setName(String name) {
this.name = name;
@Length(min=0, max=2000)
public String getHref() {
return href;
public void setHref(String href) {
this.href = href;
@Length(min=0, max=20)
public String getTarget() {
return target;
public void setTarget(String target) {
this.target = target;
@Length(min=0, max=100)
public String getIcon() {
return icon;
public void setIcon(String icon) {
this.icon = icon;
@NotNull
public Integer getSort() {
return sort;
public void setSort(Integer sort) {
this.sort = sort;
@Length(min=1, max=1)
public String getIsShow() {
return isShow;
public void setIsShow(String isShow) {
this.isShow = isShow;
@Length(min=0, max=200)
public String getPermission() {
return permission;
public void setPermission(String permission) {
this.permission = permission;
public String getParentId() {
return parent != null && parent.getId() != null ? parent.getId() : "0";
@JsonIgnore
public static void sortList(List<Menu> list, List<Menu> sourcelist, String parentId, boolean cascade){
for (int i=0; i<sourcelist.size(); i++){
Menu e = sourcelist.get(i);
if (e.getParent()!=null && e.getParent().getId()!=null
&& e.getParent().getId().equals(parentId)){
list.add(e);
if (cascade){
// 判断是否还有子节点, 有则继续获取子节点
for (int j=0; j<sourcelist.size(); j++){
Menu child = sourcelist.get(j);
if (child.getParent()!=null && child.getParent().getId()!=null
&& child.getParent().getId().equals(e.getId())){
sortList(list, sourcelist, e.getId(), true);
break;
@JsonIgnore
public static String getRootId(){
return "1";
public String getUserId() {
return userId;
public void setUserId(String userId) {
this.userId = userId;
@Override
public String toString() {