Javascript 如何从node.js中的req.body获取json数组?

Javascript 如何从node.js中的req.body获取json数组?,javascript,node.js,json,body-parser,stringify,Javascript,Node.js,Json,Body Parser,Stringify,我想向节点js发布一个json数组。我使用AJAXPOST方法做了同样的事情,在服务器端,我尝试使用req.body访问同样的内容。但结果是字符串形式的。因此,我无法逐个迭代json数组元素。我如何才能做到这一点?json.parse或stringyfy所有内容都显示为object。这是在req.body中获得的 req.body: '[{"img_id":"img_1","name":"abc","source":"img/Icon_ABc. png"},{"img_id":"

我想向节点js发布一个json数组。我使用AJAXPOST方法做了同样的事情,在服务器端,我尝试使用req.body访问同样的内容。但结果是字符串形式的。因此,我无法逐个迭代json数组元素。我如何才能做到这一点?json.parse或stringyfy所有内容都显示为object。这是在req.body中获得的

      req.body: '[{"img_id":"img_1","name":"abc","source":"img/Icon_ABc.
  png"},{"img_id":"img_0","name":"flower","source":"img/Icon_flower.png"}, 
  {"img_id":"img_5","name":"panda","source":"img/Icon_panda.png"
  }]'
我使用的post方法是ajax

     Dataform.append("finalAray",JSON.stringify(finalArray))
 $.ajax({
    url: '/api/upload/',
    type: 'POST',
    processData: false,
    contentType: false,
    dataType : 'application/json; charset=utf-8',
    data: Dataform,

    success: function(data){
       //................
    },
    error: function(exception){
        alert('error:'+exception);
    }
});

感谢您的测试和更新。我假设您只发布JSON,而您发布的是表单数据,其中包括JSON

我认为在这种情况下,最好的方法是使用模块解析上传的数据,如下所示:

server.js

const express = require("express");
const app = express();
const port = 3000;
const multer  = require('multer')
const upload = multer();

app.use(express.static("./"));


app.post('/', upload.any(), function(req,res){
    console.log('Received files from client: ', req.files);
    console.log('Received form data from client: ', req.body);

    console.log('Iterating array:');
    let array = JSON.parse(req.body.finalAray);
    array.forEach((element, index) => {
        console.log(`Element at [${index}]: ${element}`)
    });
    res.json( { "status": "ok" } );
})


app.listen(port, function(){
    console.log(`Example app listening at http://localhost:${port}`)
})
客户端

{     
Dataform.append("finalAray",JSON.stringify(finalArray))
 $.ajax({
    url: '/api/upload/',
    type: 'POST',
    processData: false,
    contentType: false,
    data: Dataform,

    success: function(data){
       //................
    },
    error: function(exception){
        alert('error:'+exception);
    }
});

由于我无法从req.body获取数组,所以我将JSON数组作为查询参数发送到ajax post的url中。因此,我在服务器的req.query中获得了字符串形式的数组。然后我解析了它,并能够进行迭代

  $.ajax({
    url: '/api/upload/?finalArray='+JSON.stringify(finalArray),
    type: 'POST',
    processData: false,
    contentType: false,
    dataType : 'application/json; charset=utf-8',
    data: fileform,
     success: function(data){
      // ........
    },
    error: function(exception){
        alert('error:'+exception);
    }
});
在服务器中

 array=req.query.finalArray;
 JSON.parse(array);
 array.forEach(function(element,index){
        console.log(element);
        console.log(index);
 });

但是我没有在迭代中获得值。req.body是如问题所示获得的。在迭代过程中,我得到的错误是req.body。for each不是一个函数@TerryHey,@Aparna,非常感谢您尝试我的代码!你能在服务器上做一个console.log(req.body)并看看它显示了什么吗?谢谢[{“img_id”:“img_1”,“name”:“abc”,“source”:“img/Icon_abc.png”},{“img_id”:“img_0”,“name”:“flower”,“source”:“img/Icon_flower.png”},{“img_id”:“img_5”,“name”:“panda”,“source”:“img/Icon_panda.png”}@Terry--req.body用单引号括起来。所以它是一个字符串。所以我不能迭代。我已经用我的ajax post调用编辑了这个问题。内容类型设置为false。当我将内容类型设置为application/json时,不会上载与表单数据对象中的json一起附加的文件。@TerryI使用req中的查询来传递json,而不是正文。它正在工作。我不知道这是否是正确的实施方式。但它正在解决我的问题。谢谢您尝试此代码。@Terry..我将用新代码编辑我的问题。请查收