本人新手,对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) )
获取到的数据如下:
想要实现的数据格式:
直接写对象最后再来执行 JSON.stringify 就可以
如果一定要这样写 那就在 crulearr.push 的时候先 parse 了
crulearr.push(JSON.parse('{"rules":[{"field":"ParentID","op":"equal","value":"'+this.ipboxid+'","type":"int"},{"field":"CarbinCode","op":"equal","value":"'+this.cabincodearr+'","type":"string"}],"op":"and"}'))
首先你这图片过小根据无法正常预览(看不清),关于 json 的知识点可以参考下(基本上通俗易懂):
多谢提醒,已经将图片换成清晰的了;
感谢指导,自己了解json数据拼接,尝试进行json字符串与json对象之间的转换;