查看: 455|回复: 0

[文章教程] 多维数组转JSON方法

[复制链接]
xuanxiao 发表于 2022-3-16 19:36:14 | 显示全部楼层 |阅读模式

  1. function arrayToJsonString(o,name) {
  2. var len = o.length;
  3. var new_arr = new Array();
  4. var str = '',strone='',strs='',jsonstr='';
  5. for(var i = 0;i<len;i++){
  6. new_arr = o[i];
  7. for(var k in new_arr){
  8. strone += '"'+k+'"'+':'+'"'+new_arr[k]+'"'+',';
  9. }
  10. str = '{'+strone.substring(0,strone.length-1)+'}';
  11. strone='';
  12. strs += str+',';&#160;
  13. new_arr=[];&#160;
  14. }
  15. strs = '['+strs.substring(0,strs.length-1)+']';
  16. strs='{"'+name+'":'+strs+'}';
  17. return strs;&#160;
  18. }
复制代码


调动方法:

  1. arrayToJsonString(data_Attach,"data_Attach"),
复制代码


结果:
  1. data_Attach: {"data_Attach":[{"0":"执行机构","1":"Nucom-10NM-20kgf.m  220V","2":"304+PTFE","3":"1","4":"0","5":"1","6":" "},{"0":"执行机构","1":"Nucom-10NS-5kgf.m   220V","2":"CD4MnCu","3":"2","4":"0","5":"2","6":" "},{"0":"手轮","1":"√","2":"CD4MnCu","3":"3","4":"0","5":"3","6":" "}]}
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号