如何传递数组值?

时间:2016-03-18 06:48:22

标签: javascript arrays node.js postman

我们如何在邮递员中传递如下所示的数组 我们如何使用express?

在节点JavaScript中获取此数组

以邮递员传递数组:

data : [
            {
                name: 'ABC',
                amount: '1500',                     
            },
            {
                name: 'NNS',
                amount: '5800',                     
            },
            {
                name: 'GED',
                amount: '3500',                     
            },
            {
                name: 'PQR',
                amount: '5500',                     
            }
        ]

修改: //在app.js中

app.use( bodyParser.json() );       // to support JSON-encoded bodies
    app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
      extended: true
    })); 

enter image description here

// users.js {router}

router.post('/test', function(req, res, next){

  console.log(req.body.data);
  res.send(req.body.data);
});

2 个答案:

答案 0 :(得分:2)

您可以按照以下步骤向邮递员发送数组:

enter image description here

你的json必须是这样的:

    {
    "data" : [
                {   "name": "ABC",
                    "amount": 1500                    
                },
                {   "name": "NNS",
                    "amount": 5800                    
                },
                {   "name": "GED",
                    "amount": 3500                    
                },
                {   "name": "PQ",
                    "amount": 5500                    
                }
        ]
    }

您可以在节点(express)中检索此POST查询,如下所示:

    var bodyParser = require('body-parser')
    app.use( bodyParser.json() );       // to support JSON-encoded bodies
    app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
      extended: true
    })); 

    app.post('/people', function(req, res, next) {
      //show received data
       console.log(req.body.data);
    });

答案 1 :(得分:0)

发送数据参数

key     => value
data[]  =>  {"name": "ABC", "amount": "1500"}
data[]  =>  {"name": "ABC", "amount": "1500"}
data[]  =>  {"name": "ABC", "amount": "1500"}

但我认为这会将值转换为字符串[" {name:value}",...]