简体   繁体   English

如何以邮递员形式数据发送对象数组并作为对象数组接收

[英]How to send array of objects in postman form data and and receive as array of objects

In postman, I have done in form-data KEY -products VALUE-在邮递员中,我已经完成了表单数据 KEY -products VALUE-

[
  { name: "Pizza", price: "10", quantity: "7" },
  { name: "Cerveja", price: "12", quantity: "5" },
  { name: "Hamburguer", price: "10", quantity: "2" },
  { name: "Fraldas", price: "6", quantity: "2" },
];

In code,I want to receive this same array of objects but I can receive it as a string.在代码中,我想接收相同的对象数组,但我可以将其作为字符串接收。 My code is我的代码是

var products =req.body.products;
console.log(typeof(products))//string shows
var Products=JSON.parse(JSON.stringify(products))
console.log(typeof(Products))// it also string shows

if I print array 0 index value it prints "[" array bracket Please Help me out I am new in this.如果我打印数组 0 索引值,它会打印“[”数组括号请帮帮我,我是新手。 Thank you谢谢

I think you have ask duplicate question Click here !我想你问了重复的问题点击这里 that solution worked for me!该解决方案对我有用!

Since products is a string, you only need to parse this into an object with JSON.parse()由于products是一个字符串,因此您只需要使用JSON.parse()将其解析为一个对象

Use JSON.stringify() when u want to convert a javascript object to a string.当您想将 javascript 对象转换为字符串时,请使用JSON.stringify()

So:所以:

var products = JSON.parse(req.body.products); // parse string to object
console.log(products[0]); // will output the pizza

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM