我在body参数
中发送以下文字 [{"full_image":"alskdfhlkjasvbuialsdvlasljkvbaslvjhbalfdghbakjldfgjlajkshfiuqr","type":"jpg"},{"full_image": "alskdfhlkjasvbuialsdvlasljkvbaslvjhbalfdghbakjldfgjlajkshfiuqr","type":"jpg"}]
然后解析它JSON.pasre
但我无法得到lenght
并循环json。以下是代码。
var images = req.body.images;
JSON.parse(images, function(key,value){
var counter = key.length;
var seaWeedPicture = {};
var base64FullImage = value;
seaweedfs.write(new Buffer(base64FullImage, 'base64')).then(function (pic) {
var seaWeedPicture = {
picture: config.seaWeedURL + '/' + pic.fid
};
}).catch(function(err){
console.log(err);
res.format({
json: function () {
res.send({
status: 200,
data: []
});
}
});
});
生成的错误为[TypeError: must start with number, buffer, array or string]
答案 0 :(得分:1)
您可以通过
获取长度Object
构造函数
var length=Object.keys(images).length;
//Do your loop with `length`
console.log(Object.keys(images).length);//2
答案 1 :(得分:1)
var record = [{"full_image":"alskdfhlkjasvbuialsdvlasljkvbaslvjhbalfdghbakjldfgjlajkshfiuqr","type":"jpg"},{"full_image": "alskdfhlkjasvbuialsdvlasljkvbaslvjhbalfdghbakjldfgjlajkshfiuqr","type":"jpg"}]
console.log(record.length);
for(var i=0;i<record.length;i++){console.log(record[i]['full_image']);}
这样做: