第一种种拼接json的key会过滤双引号 var arr = "sequence" + ":" + "["; let arrEnd = "]"; for(let i=0;i<[1,2,3].length;i++){ arr += "{" +"sequence" + ":" + [1,2,3][i] + "," + "process" + "[" + [[1,2,3],[2,4],[5,4]][i]+"]"+"}"+","; } let arrs = '{' + arr.substring(0,arr.length-1)+arrEnd + '}'; console.log(arrs);//{sequence:[{sequence:1,process[1,2,3]},{sequence:2,process[2,4]},{sequence:3,process[5,4]}]} 第二种拼接数组[1,2,3]会被当字符串处理 var arr = "sequence" + ":" + "["; let arrEnd = "]"; for(let i=0;i<[1,2,3].length;i++){ arr += "{ \"sequence\": [1,2,3][i], \"process\": [[[1,2,3],[2,4],[5,4]][i]]}"+","; } let arrs = JSON.stringify('{' + arr.substring(0,arr.length-1)+arrEnd + '}'); console.log(JSON.parse(arrs)); /*{sequence:[{ "sequence": [1,2,3][i], "process": [[[1,2,3],[2,4],[5,4]][i]]},{ "sequence": [1,2,3][i], "process": [[[1,2,3],[2,4],[5,4]][i]]},{ "sequence": [1,2,3][i], "process": [[[1,2,3],[2,4],[5,4]][i]]}]}*/求一种拼接完能和json格式一样的方法,然后要能取的到值
添加回答
举报
0/150
提交
取消