Js 数组转JSON格式+uni多图上传
var arrays=new Array(); //JSON.stringify() 不能转下标非1,2,3这种数字的数组, //所以采用新建对象的方式添加数据
var products={};
products['productid']='32';
products['name']='手机';
arrays.push(products);
var products2={};
products2['productid']='33';
products2['name']='电脑';
arrays.push(products2);
var json_arrays = JSON.stringify(arrays);
console.log(json_arrays);
// 选择图片
chooseImage: async function() {
if (this.imageList.length === 9) {
return;
}
uni.chooseImage({
sourceType: sourceType[this.sourceTypeIndex],
sizeType: sizeType[this.sizeTypeIndex],
count: this.imageList.length + this.count[this.countIndex] > 9 ? 9 - this.imageList.length :
this.count[this.countIndex],
success: (res) => {
this.imageList = this.imageList.concat(res.tempFilePaths);
var arrays = new Array();
var jsonDatas = {};
console.log(this.imageList.length,'照片数量');
console.log( res.tempFilePaths[0])
wx.getFileSystemManager().readFile({
filePath: res.tempFilePaths[0], //选择图片返回的相对路径
encoding: 'base64', //编码格式
success: res => { //成功的回调
//console.log('data:image/png;base64,' + res.data)
for(var i =0;i<this.imageList.length;i++){
var data = 'data:image/png;base64,' + res.data
jsonDatas['urlp'+(i+1)]=data;
arrays.push(jsonDatas);
}
var imgJson = JSON.stringify(arrays);
this.jsonData=imgJson
}
})
}
});
},