I_nur
chenjin2136
2019-11-17 23:24
采纳率: 100%
浏览 349

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

本人新手,对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) )

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • tiankongzhichenglyf
    tkzc_shark 2019-11-18 09:59
    已采纳
        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));
    
    点赞 评论
  • zqbnqsdsmd
    zqbnqsdsmd 2019-11-18 10:39
    点赞 评论

相关推荐