给的json数据中,有很多层的对象,将对象嵌套多层不直观,所以一般我们将其转换为多维数组进行操作,转换方法便是遍历对象,然后将对象的每个属性挂载到该层级上,如果对象的属性具有子属性,则继续进行嵌套操作.具体操作:
'use strict'; function convert(obj,keys,arr){ for (let i in obj){ if(typeof obj[i] ==='object'){ convert(obj[i],typeof obj[i] === 'object'? keys.concat(i) : keys,arr); }else{ typeof arr[keys.length-1]==='object'? arr[keys.length-1][i]=obj[i]:arr[keys.length-1]=obj[i]; } } } let json = {header: {status: "1", markid: ""}, body: {videofile: {infos: { mp4_180: { backUrl2: "http://play.g3proxy.lecloud.com/vod/v2/MTYyLzMyLzU1L2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMDE2MDYxNjkxLWF2Yy0xMjgzMzgtYWFjLTMyMDAwLTI3NjkwNjctNTc4NDc4OTItY2RkYWExZWFjYzE4MWYyZjQwYTYyMzQ0OTRmZWM0MDYtMTQ1MTg4MzU1MjQ2Ny5tcDQ=?b=167&mmsid=41080393&tm=1453967403&key=44316f829b240caa11fca65c6e479481&platid=3&splatid=347&playid=0&tss=no&vtype=58&cvid=768876645571&payff=0&pip=fc086fb11a6f4a36ce405a1bdbbf16d4&retry=1&format=1&sign=mb&dname=mobile&expect=3&p1=0&p2=00&p3=003&tag=mobile&pid=", filesize: "57847892", backUrl1: "http://117.121.54.219/vod/v2/MTYyLzMyLzU1L2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMDE2MDYxNjkxLWF2Yy0xMjgzMzgtYWFjLTMyMDAwLTI3NjkwNjctNTc4NDc4OTItY2RkYWExZWFjYzE4MWYyZjQwYTYyMzQ0OTRmZWM0MDYtMTQ1MTg4MzU1MjQ2Ny5tcDQ=?b=167&mmsid=41080393&tm=1453967403&key=44316f829b240caa11fca65c6e479481&platid=3&splatid=347&playid=0&tss=no&vtype=58&cvid=768876645571&payff=0&pip=fc086fb11a6f4a36ce405a1bdbbf16d4&format=1&sign=mb&dname=mobile&expect=3&p1=0&p2=00&p3=003&tag=mobile&pid=", mainUrl: "http://play.g3proxy.lecloud.com/vod/v2/MTYyLzMyLzU1L2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMDE2MDYxNjkxLWF2Yy0xMjgzMzgtYWFjLTMyMDAwLTI3NjkwNjctNTc4NDc4OTItY2RkYWExZWFjYzE4MWYyZjQwYTYyMzQ0OTRmZWM0MDYtMTQ1MTg4MzU1MjQ2Ny5tcDQ=?b=167&mmsid=41080393&tm=1453967403&key=44316f829b240caa11fca65c6e479481&platid=3&splatid=347&playid=0&tss=no&vtype=58&cvid=768876645571&payff=0&pip=fc086fb11a6f4a36ce405a1bdbbf16d4&format=1&sign=mb&dname=mobile&expect=3&p1=0&p2=00&p3=003&tag=mobile&pid=", backUrl0: "http://bplay.g3proxy.lecloud.com/vod/v2/MTYyLzMyLzU1L2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMDE2MDYxNjkxLWF2Yy0xMjgzMzgtYWFjLTMyMDAwLTI3NjkwNjctNTc4NDc4OTItY2RkYWExZWFjYzE4MWYyZjQwYTYyMzQ0OTRmZWM0MDYtMTQ1MTg4MzU1MjQ2Ny5tcDQ=?b=167&mmsid=41080393&tm=1453967403&key=44316f829b240caa11fca65c6e479481&platid=3&splatid=347&playid=0&tss=no&vtype=58&cvid=768876645571&payff=0&pip=fc086fb11a6f4a36ce405a1bdbbf16d4&format=1&sign=mb&dname=mobile&expect=3&p1=0&p2=00&p3=003&tag=mobile&pid=", storePath: "162/32/55/letv-uts/14/ver_00_22-1016061691-avc-128338-aac-32000-2769067-57847892-cddaa1eacc181f2f40a6234494fec406-1451883552467.mp4" }, mp4_1300: {
登录后复制
以上就是如何将多层嵌套的JSON对象转换为易于操作的多维数组?的详细内容,更多请关注php中文网其它相关文章!