2016-09-29 294 views
0

我在身體參數解析字符串

[{"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

我僅僅指剛要循環的json,w帶着'seaweedfs'的禮儀文件並返回它的網址。 – Khan

回答

1

您可以通過Object構造得到長度

var length=Object.keys(images).length; 
//Do your loop with `length` 
console.log(Object.keys(images).length);//2 
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']);} 

這樣做: