后端传来的josn数据
[
{
"menu_id": 1,
"menu_seq": null,
"parent_id": 0,
"text": "root",
"url": null,
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3130303030303030303030305430372d31342031363a34328dc42df7b84e7fcf622675ee8a70d92af4ee345dbc2a72c800b5e1a291939a",
"appname": null,
"children": [
{
"menu_id": 2,
"menu_seq": 1,
"parent_id": 1,
"text": "党建组织",
"url": "",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3230303030303030303030305430372d31342031363a3432b010e98c54a2f3dd7e09031ffe0b364263424f46f20a13902b3a35252f4142",
"appname": "hsdj",
"children": [
{
"menu_id": 5,
"menu_seq": 1,
"parent_id": 2,
"text": "党员管理",
"url": "djdy/djdy.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3530303030303030303030305430372d31342031363a3432e9434cfce2b897e53a3fc8233ef2b968952dcc864ebe838abf9c72801c42ca",
"appname": "hsdj",
"children": []
},
{
"menu_id": 7,
"menu_seq": 1,
"parent_id": 2,
"text": "组织架构图",
"url": "djzz/zzjg/zzjg.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3730303030303030303030305430372d31342031363a34322bf540335f12111b104824157005aa09c3f5ec38fe5784b4bf90d83c40be2e",
"appname": "",
"children": []
},
{
"menu_id": 3,
"menu_seq": 22,
"parent_id": 2,
"text": "组织管理",
"url": "djzz/djzz.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3330303030303030303030305430372d31342031363a34321f8411c7a8ced1d84ae836e2d3a5a15d06f7e7a5acde868694ec84884015da",
"appname": "hsdj",
"children": []
}
]
},
{
"menu_id": 10,
"menu_seq": 3,
"parent_id": 1,
"text": "党建学习教育",
"url": "",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3130303030303030303030305430372d31342031363a34321910915038350866dc1f631e64f28b71af45000e34604f0e856a1f1c3a1ac8",
"appname": "hsdj",
"children": [
{
"menu_id": 49,
"menu_seq": 1,
"parent_id": 10,
"text": "我的党建学习",
"url": "xxjy/xxrw/xxrw_panl.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3439303030303030303030305430372d31342031363a3432b2ba95afbb3a6bdb0ada2a57049faab12aa72a0ea3f37f6a405a9fe79799f0",
"appname": "hsdj",
"children": []
},
{
"menu_id": 50,
"menu_seq": 2,
"parent_id": 10,
"text": "党建学习计划",
"url": "xxjy/xxjh/xxjh.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3530303030303030303030305430372d31342031363a343253da737f0d41b55a1538b5ef2a7af10644307a1ee06e35a2b4deb91d53d774",
"appname": "hsdj",
"children": []
},
{
"menu_id": 51,
"menu_seq": 3,
"parent_id": 10,
"text": "党建学习总结",
"url": "xxjy/xxzj/xxzj.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3531303030303030303030305430372d31342031363a34327537966cedffcc762ad47aa52f882064cf79abc97bb68c0a23955fee28cbb2",
"appname": "hsdj",
"children": []
},
{
"menu_id": 52,
"menu_seq": 4,
"parent_id": 10,
"text": "党建题库管理",
"url": "xxjy/tkgl/tkgl.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3532303030303030303030305430372d31342031363a3432d3d6097ad3d364db7c9164f2cac408c2642c48756ce249ffb2314614b6573e",
"appname": "hsdj",
"children": []
},
{
"menu_id": 53,
"menu_seq": 5,
"parent_id": 10,
"text": "党建试卷管理",
"url": "xxjy/sjgl/sjgl.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3533303030303030303030305430372d31342031363a343238d45f0eed882f3f6139d717f62e5758165b5798421010ef447e57406263e5",
"appname": "hsdj",
"children": []
},
{
"menu_id": 54,
"menu_seq": 6,
"parent_id": 10,
"text": "党建知识库管理",
"url": "xxjy/zskgl/zskgl.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3534303030303030303030305430372d31342031363a34321d022e1f1800237030336b23e546d30453dfd08fb5d33ac7c30a5fb4c97b74",
"appname": "hsdj",
"children": []
},
{
"menu_id": 55,
"menu_seq": 7,
"parent_id": 10,
"text": "党建资讯库管理",
"url": "xxjy/djzixun/djzixun.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3535303030303030303030305430372d31342031363a3432309cd1bea50560bbd013748b62b2b909b548fd84c7a906b85449fc40fbee2f",
"appname": "hsdj",
"children": []
},
{
"menu_id": 57,
"menu_seq": 8,
"parent_id": 10,
"text": "党建标签管理",
"url": "xxjy/bqgl/bqgl.html",
"authcode": "1,1,1,1",
"optcode": "c,r,u,d",
"accessKey": "3537303030303030303030305430372d31342031363a3432bd2e8b9601a029855cee6dc0a93cdaf4d9841eb4d33ed69061090c6a06ff95",
"appname": "hsdj",
"children": []
}
]
}
]
}
]
实体类对象(用来接收数据)
package com.example.hsdj;
import android.app.Application;
import java.util.List;
public class MenuApplication extends Application {
private int menu_id;
private Object menu_seq;
private int parent_id;
private String text;
private Object url;
private String authcode;
private String optcode;
private String accessKey;
@Override
public String toString() {
return "MenuApplication{" +
"menu_id=" + menu_id +
", menu_seq=" + menu_seq +
", parent_id=" + parent_id +
", text='" + text + '\'' +
", url=" + url +
", authcode='" + authcode + '\'' +
", optcode='" + optcode + '\'' +
", accessKey='" + accessKey + '\'' +
", appname=" + appname +
", children=" + children +
'}';
}
private Object appname;
private List<MenuApplication> children;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
public int getMenu_id() {
return menu_id;
}
public void setMenu_id(int menu_id) {
this.menu_id = menu_id;
}
public Object getMenu_seq() {
return menu_seq;
}
public void setMenu_seq(Object menu_seq) {
this.menu_seq = menu_seq;
}
public int getParent_id() {
return parent_id;
}
public void setParent_id(int parent_id) {
this.parent_id = parent_id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Object getUrl() {
return url;
}
public void setUrl(Object url) {
this.url = url;
}
public String getAuthcode() {
return authcode;
}
public void setAuthcode(String authcode) {
this.authcode = authcode;
}
public String getOptcode() {
return optcode;
}
public void setOptcode(String optcode) {
this.optcode = optcode;
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
this.accessKey = accessKey;
}
public Object getAppname() {
return appname;
}
public void setAppname(Object appname) {
this.appname = appname;
}
public List<MenuApplication> getChildren() {
return children;
}
public void setChildren(List<MenuApplication> children) {
this.children = children;
}
}
递归解析算法
//递归:解析menuTree
private MenuApplication digui(JSONObject json) throws JSONException {
MenuApplication menuApplication=new MenuApplication();
menuApplication.setMenu_id(json.optInt("menu_id"));
menuApplication.setMenu_seq(json.optString("menu_seq"));
menuApplication.setParent_id(json.optInt("parent_id"));
menuApplication.setText(json.optString("text"));
menuApplication.setUrl(json.optString("url"));
menuApplication.setAuthcode(json.optString("authcode"));
menuApplication.setOptcode(json.optString("optcode"));
menuApplication.setAccessKey(json.optString("accessKey"));
menuApplication.setAppname(json.optString("appname"));
if(json.optString("children")!=null){
JSONArray jsonArray= new JSONArray(json.optString("children"));
List<MenuApplication> childrens=new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject=new JSONObject(jsonArray.get(i).toString());
childrens.add(digui(jsonObject));
}
menuApplication.setChildren(childrens);
}
return menuApplication;
}
递归解析数据样式
{
menu_id=1,
menu_seq=null,
parent_id=0,
text='root',
url=null,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3130303030303030303030305430372d31342031393a3239dcf1c7eb33a73e8f871429f8087201e38c73a2e37cdad66490ebb53d586e4f',
appname=null,
children=[
MenuApplication{
menu_id=2,
menu_seq=1,
parent_id=1,
text='党建组织',
url=,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3230303030303030303030305430372d31342031393a3239f95106ce0d96f23c118fee7c4793bb8031b935c42cd58f3291d2be281805da',
appname=hsdj,
children=[
MenuApplication{
menu_id=7,
menu_seq=1,
parent_id=2,
text='组织架构图',
url=djzz/zzjg/zzjg.html,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3730303030303030303030305430372d31342031393a3239b03f087bc40e52166978bfca58f140648d0aa1c7a249d90128cf5f8df02c84',
appname=,
children=[]
},
MenuApplication{
menu_id=5,
menu_seq=1,
parent_id=2,
text='党员管理',
url=djdy/djdy.html,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3530303030303030303030305430372d31342031393a3239e56869226fba3820edc56bdc80d577de7e036d7264c70999c5f76eb472e3ed',
appname=hsdj,
children=[]
},
MenuApplication{
menu_id=3,
menu_seq=22,
parent_id=2,
text='组织管理',
url=djzz/djzz.html,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3330303030303030303030305430372d31342031393a3239e74363c190cd2fd15351a917ded1224ebff2c13348ed6e474355d98126fbf9',
appname=hsdj,
children=[]
}
]
},
MenuApplication{
menu_id=10,
menu_seq=3,
parent_id=1,
text='党建学习教育',
url=, authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3130303030303030303030305430372d31342031393a3239e320c6bebedd0aec7cc96d2e4abea61674d09ef3434874f5e6d67e8e81aecd',
appname=hsdj,
children=[
MenuApplication{
menu_id=49,
menu_seq=1,
parent_id=10,
text='我的党建学习',
url=xxjy/xxrw/xxrw_panl.html,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3439303030303030303030305430372d31342031393a3239f8a2bec41bb440af5e781a73371e733d0a24ba4e47a2a4527a249ea2070374',
appname=hsdj,
children=[]
},
MenuApplication{
menu_id=50,
menu_seq=2,
parent_id=10,
text='党建学习计划',
url=xxjy/xxjh/xxjh.html,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3530303030303030303030305430372d31342031393a3239b987fa136b2df67e4dafcf529931587c749c001cf8a6fb7a1dfdf8b711ac2f',
appname=hsdj,
children=[]
},
MenuApplication{
menu_id=51,
menu_seq=3,
parent_id=10,
text='党建学习总结',
url=xxjy/xxzj/xxzj.html,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3531303030303030303030305430372d31342031393a3239aac9baf8e08ad4adc83ae006e786714296e6ff6dab910b46ffbbdfeed8bb47',
appname=hsdj,
children=[]
},
MenuApplication{
menu_id=52,
menu_seq=4,
parent_id=10,
text='党建题库管理',
url=xxjy/tkgl/tkgl.html,
authcode='1,1,1,1',
optcode='c,r,u,d',
accessKey='3532303030303030303030305430372d31342031393a3239b6a81da70be0469cde0896fd2a98eec4bde1fabf3531055aac47daaffc6be0',
appname=hsdj,
children=[]
},
MenuApplication{
menu_id=53, menu_seq=5, parent_id=10, text='党建试卷管理', url=xxjy/sjgl/sjgl.html, authcode='1,1,1,1', optcode='c,r,u,d', accessKey='3533303030303030303030305430372d31342031393a3239bba5296c0a4520b0b0697e39c7fd2c91ce75447562eb3204250a051e241ab5', appname=hsdj, children=[]}, MenuApplication{menu_id=54, menu_seq=6, parent_id=10, text='党建知识库管理', url=xxjy/zskgl/zskgl.html, authcode='1,1,1,1', optcode='c,r,u,d', accessKey='3534303030303030303030305430372d31342031393a3239f35041cdfd4bf3362c018f1e18f48e6675f6a1144bfa5ba0f9ab3848d64866', appname=hsdj, children=[]}, MenuApplication{menu_id=55, menu_seq=7, parent_id=10, text='党建资讯库管理', url=xxjy/djzixun/djzixun.html, authcode='1,1,1,1', optcode='c,r,u,d', accessKey='3535303030303030303030305430372d31342031393a3239c3661abe6c2c4b445f9db624c81bf1bdf9b98af11166afbea85d7f755cf195', appname=hsdj, children=[]}, MenuApplication{menu_id=57, menu_seq=8, parent_id=10, text='党建标签管理', url=xxjy/bqgl/bqgl.html, authcode='1,1,1,1', optcode='c,r,u,d', accessKey='3537303030303030303030305430372d31342031393a32390ee2290959b655204d3ace643c936f956507570228e665460b9a3d14b18a04', appname=hsdj, children=[]