从urlencode解析字符串然后循环

时间:2016-09-29 07:20:16

标签: json node.js

我在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]

2 个答案:

答案 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']);}

这样做: