如何递归获取json里末级章节名称

漂流的河 发布于 2016/01/26 10:56
阅读 263
收藏 0
这是我获取到的一个大JSON,里面有7个大章节,现在我要获取每个大章节里的末级章节名称,如何获取?递归吗? [{ "status":200, "data":[{ "AppID": 133, "BookID": 1, "ID": 1, "ChapterName": "测试图书章节1", "PageNo": 1, "PID": 0, "SortID": 0, "OperateUserID": 1, "OperateTime": "2015-12-17T14:25:08.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 3, "ChapterName": "测试图书章节34", "PageNo": 0, "PID": 1, "SortID": 1, "OperateUserID": 0, "OperateTime": "2015-12-15T08:45:21.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 19, "ChapterName": "ceshi1", "PageNo": 23, "PID": 3, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T11:53:25.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 28, "ChapterName": "zengjiaziji21", "PageNo": 21, "PID": 19, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T11:56:49.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 21, "ChapterName": "zijiceshi01", "PageNo": 14, "PID": 19, "SortID": 2, "OperateUserID": 1, "OperateTime": "2015-12-17T11:56:38.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 37, "ChapterName": "cesef", "PageNo": 342, "PID": 19, "SortID": 3, "OperateUserID": 1, "OperateTime": "2015-12-17T12:20:35.000Z", "Childs": null }] }, { "AppID": 133, "BookID": 1, "ID": 25, "ChapterName": "增加同级", "PageNo": 112, "PID": 3, "SortID": 2, "OperateUserID": 1, "OperateTime": "2015-12-17T11:52:35.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 26, "ChapterName": "增加子级", "PageNo": 34, "PID": 25, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T09:50:53.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 32, "ChapterName": "cehsitied", "PageNo": 31, "PID": 25, "SortID": 2, "OperateUserID": 1, "OperateTime": "2015-12-17T12:12:32.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 36, "ChapterName": "ze4", "PageNo": 344, "PID": 25, "SortID": 3, "OperateUserID": 1, "OperateTime": "2015-12-17T12:18:05.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 39, "ChapterName": "feewe", "PageNo": 0, "PID": 25, "SortID": 4, "OperateUserID": 1, "OperateTime": "2015-12-17T12:21:13.000Z", "Childs": null }] }, { "AppID": 133, "BookID": 1, "ID": 4, "ChapterName": "测试图书章节4", "PageNo": 2, "PID": 3, "SortID": 3, "OperateUserID": 0, "OperateTime": "2015-12-15T09:08:45.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 27, "ChapterName": "zengjia1", "PageNo": 11, "PID": 4, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T11:50:06.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 29, "ChapterName": "cehsiti", "PageNo": 31, "PID": 4, "SortID": 2, "OperateUserID": 1, "OperateTime": "2015-12-17T12:11:52.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 30, "ChapterName": "cehsitongjiee", "PageNo": 11, "PID": 4, "SortID": 3, "OperateUserID": 1, "OperateTime": "2015-12-17T12:12:05.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 31, "ChapterName": "cehsitie", "PageNo": 31, "PID": 4, "SortID": 4, "OperateUserID": 1, "OperateTime": "2015-12-17T12:12:22.000Z", "Childs": null }] }, { "AppID": 133, "BookID": 1, "ID": 33, "ChapterName": "cehsitongjieee", "PageNo": 111, "PID": 3, "SortID": 4, "OperateUserID": 1, "OperateTime": "2015-12-17T12:15:36.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 34, "ChapterName": "zengjia11", "PageNo": 0, "PID": 33, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T12:17:33.000Z", "Childs": null }] }, { "AppID": 133, "BookID": 1, "ID": 35, "ChapterName": "ze", "PageNo": 34, "PID": 3, "SortID": 5, "OperateUserID": 1, "OperateTime": "2015-12-17T12:17:41.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 38, "ChapterName": "fe", "PageNo": 0, "PID": 35, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T12:21:04.000Z", "Childs": null }] }, { "AppID": 133, "BookID": 1, "ID": 40, "ChapterName": "gr", "PageNo": 0, "PID": 3, "SortID": 6, "OperateUserID": 1, "OperateTime": "2015-12-17T12:21:21.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 42, "ChapterName": "grhrtjkt6", "PageNo": 0, "PID": 3, "SortID": 7, "OperateUserID": 1, "OperateTime": "2015-12-17T12:50:42.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 50, "ChapterName": "章节433", "PageNo": 1232, "PID": 3, "SortID": 8, "OperateUserID": 1, "OperateTime": "2015-12-17T12:52:11.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 51, "ChapterName": "子级子级", "PageNo": 23, "PID": 3, "SortID": 9, "OperateUserID": 1, "OperateTime": "2015-12-17T12:52:46.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 41, "ChapterName": "grhrt", "PageNo": 0, "PID": 3, "SortID": 10, "OperateUserID": 1, "OperateTime": "2015-12-17T14:24:49.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 49, "ChapterName": "tongjie", "PageNo": 123, "PID": 41, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T12:51:09.000Z", "Childs": null }] }, { "AppID": 133, "BookID": 1, "ID": 52, "ChapterName": "同级同级", "PageNo": 3, "PID": 3, "SortID": 11, "OperateUserID": 1, "OperateTime": "2015-12-17T12:53:03.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 73, "ChapterName": "ceshi2", "PageNo": 22, "PID": 3, "SortID": 12, "OperateUserID": 1, "OperateTime": "2015-12-18T00:18:20.000Z", "Childs": null }] }] }, { "AppID": 133, "BookID": 1, "ID": 6, "ChapterName": "测试图书章节6", "PageNo": 0, "PID": 0, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T14:25:09.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 2, "ChapterName": "测试图书章节2", "PageNo": 1, "PID": 0, "SortID": 2, "OperateUserID": 1, "OperateTime": "2015-12-17T14:25:06.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 8, "ChapterName": "测试名称", "PageNo": 0, "PID": 0, "SortID": 3, "OperateUserID": 1, "OperateTime": "2015-12-17T14:25:05.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 9, "ChapterName": "测试名称3", "PageNo": 12, "PID": 0, "SortID": 4, "OperateUserID": 1, "OperateTime": "2015-12-17T14:25:03.000Z", "Childs": [{ "AppID": 133, "BookID": 1, "ID": 56, "ChapterName": "测试名称3子级", "PageNo": 23, "PID": 9, "SortID": 1, "OperateUserID": 1, "OperateTime": "2015-12-17T14:09:43.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 57, "ChapterName": "子级1", "PageNo": 11, "PID": 9, "SortID": 2, "OperateUserID": 1, "OperateTime": "2015-12-17T14:10:46.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 58, "ChapterName": "子级12", "PageNo": 112, "PID": 9, "SortID": 3, "OperateUserID": 1, "OperateTime": "2015-12-17T14:10:55.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 61, "ChapterName": "www", "PageNo": 11, "PID": 9, "SortID": 6, "OperateUserID": 1, "OperateTime": "2015-12-17T14:13:08.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 63, "ChapterName": "atter", "PageNo": 0, "PID": 9, "SortID": 8, "OperateUserID": 1, "OperateTime": "2015-12-17T14:17:07.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 64, "ChapterName": "atterq", "PageNo": 0, "PID": 9, "SortID": 9, "OperateUserID": 1, "OperateTime": "2015-12-17T14:20:48.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 65, "ChapterName": "atterqdf", "PageNo": 0, "PID": 9, "SortID": 10, "OperateUserID": 1, "OperateTime": "2015-12-17T14:21:46.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 67, "ChapterName": "34 s", "PageNo": 1145, "PID": 9, "SortID": 11, "OperateUserID": 1, "OperateTime": "2015-12-17T14:22:07.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 66, "ChapterName": "atterqdfcds", "PageNo": 0, "PID": 9, "SortID": 12, "OperateUserID": 1, "OperateTime": "2015-12-17T14:21:50.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 68, "ChapterName": "tt", "PageNo": 0, "PID": 9, "SortID": 13, "OperateUserID": 1, "OperateTime": "2015-12-17T14:30:54.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 70, "ChapterName": "yy", "PageNo": 0, "PID": 9, "SortID": 15, "OperateUserID": 1, "OperateTime": "2015-12-17T14:31:33.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 71, "ChapterName": "tijiao", "PageNo": 111, "PID": 9, "SortID": 16, "OperateUserID": 1, "OperateTime": "2015-12-17T14:33:21.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 76, "ChapterName": "fete", "PageNo": 0, "PID": 9, "SortID": 17, "OperateUserID": 1, "OperateTime": "2015-12-18T00:21:03.000Z", "Childs": null }] }, { "AppID": 133, "BookID": 1, "ID": 75, "ChapterName": "fe", "PageNo": 0, "PID": 0, "SortID": 5, "OperateUserID": 1, "OperateTime": "2015-12-18T02:14:55.000Z", "Childs": null }, { "AppID": 133, "BookID": 1, "ID": 74, "ChapterName": "zen1", "PageNo": 1, "PID": 0, "SortID": 6, "OperateUserID": 1, "OperateTime": "2015-12-18T02:05:41.000Z", "Childs": null }], "message":"获取章节成功!" }]
加载中
0
天机
天机

这个不需要递归,如果急用的话,先用遍历获取全部,再取出其中所需内容,不急的话,其中取其中需要的内容就行了。 


漂流的河
@天机 获取每个末级的chapterName和ID吧,^-^
天机
天机
@漂流的河 只获取其中一个参数、其它的都不用是吗?
漂流的河
@天机 我的意思是不限制级数的,实际情况里级数不会是无限的。
天机
天机
@漂流的河 无限级的,如果是的话,就算是用递归也是死循环了!22
漂流的河
遍历不行吧,它是无限级的,我这只列到4级目录而已。
0
无名developer
无名developer
递归的遍历怎么不行呢?有孩子就继续往里面找
漂流的河
代码怎么写13
0
maradona
maradona

感觉楼主的需求就是有BUG或者不明确的,需要进行需求澄清

层级是否确定?如何界定大章?如何界定末节?

源头数据不确定会导致程序不能按既定目标执行

漂流的河
末级章节就是Childs为null吧 ,不管它是第几级,只要Childs为null,都看做末级
0
maradona
maradona

获取所有么,直接递归遍历下就可以咯,如果用js写的话,没测试的代码

function getAll(json){
    var arr = [];
    _getAll(json,arr);
    return arr;
}
function _getAll(childs,arr){
    for(var item in childs){
        if(item.childs){
            _getAll(item.childs,arr);
        }else{
            arr[arr.length] = item;
        }
    }
}



0
inuxor
inuxor
歪门邪道:把这个json当字符串处理,正则。
返回顶部
顶部