js拼接对象里面含数组的json数据? 5C

本人新手,对js了解不深,js拼接对象里面含数组json字符串不知道怎么搞了,求大神指导

数组代码

      var crulearr=[]
      for(var ccode of this.newDB){
        if(ccode.id == this.ipboxid){
          crulearr.push('{"rules":[{"field":"ParentID","op":"equal","value":"'+this.ipboxid+'","type":"int"},{"field":"CarbinCode","op":"equal","value":"'+this.cabincodearr+'","type":"string"}],"op":"and"}')
        }
        else{
          crulearr.push('{"rules":[{"field":"ParentID","op":"equal","value":"'+ccode.id+'","type":"int"},{"field":"CarbinCode","op":"equal","value":"'+ccode.cabincodes+'","type":"string"}],"op":"and"}')
        }

      }
      console.log('row202:'+ JSON.stringify(crulearr))

数组数据如下:
图片说明

拼json数据代码

      var rules='{"groups":'+ JSON.stringify(crulearr) +',"op":"and"}'
      this.prule=JSON.parse(rules)
      console.log('row222:'+ JSON.stringify(this.prule) )

获取到的数据如下:
图片说明

想要实现的数据格式:
图片说明

tiankongzhichenglyf
tkzc_shark 失踪人口
6 个月之前 回复

2个回答

    var crulearr = [];
    crulearr.push(JSON.parse('{"rules":[{"field":"ParentID","op":"equal","value":"1","type":"int"},{"field":"CarbinCode","op":"equal","value":"1","type":"string"}],"op":"and"}'));
    var obj = {};
    obj.groups = crulearr;
    obj.op = "and";
    console.log(JSON.stringify(obj));
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐