[英]How do i receive post data at server same as client post data format using nodejs?
I have a post data in the format below 我有以下格式的帖子数据
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}
Expecting the same format on the server but received the format below 预计服务器上的格式相同,但收到以下格式
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[["-102.038635","36.800488"],["-94.567716","36.800488"],["-96.149793","43.381098"],["-104.060178","43.508721"]]}}}
You can see the difference in coordinates
property. 您可以看到coordinates
属性的差异。 Double quotes are added to each value in server received coordinates
array. 将双引号添加到服务器接收的coordinates
数组中的每个值。 How can I get the coordinates
array same as client post data? 如何获得与客户发布数据相同的coordinates
数组?
My application configured with expressJS and using body-parser. 我的应用程序配置了expressJS并使用body-parser。 Any additional settings required to fix the problem? 要解决此问题还需要其他设置吗?
app.use(bodyParser.urlencoded({ limit: '50mb' }));
app.use(bodyParser.json({ limit: '50mb' }));
Thanks in advance. 提前致谢。
First of all, from @bumblebeen , HTTP understands everything as a string and as such converts everything to a string. 首先,从@bumblebeen ,HTTP将所有内容都理解为字符串,并将所有内容转换为字符串。 It's up to you to parse it using Number.parseInt
由您决定使用Number.parseInt
解析它
Also, it is irrelevant but express now ships with it's own body parser which you can use. 同样,它无关紧要,但是express现在附带了它自己的身体解析器,您可以使用它。 So, rather than calling bodyParser.json
, you simply call express.json
因此,您bodyParser.json
调用bodyParser.json
,而只需调用express.json
即可express.json
Per the comment stated, you could have a function called parseCoordinates
根据说明,您可以拥有一个名为parseCoordinates
的函数
function parseCoordinates(coordinates) {
return coordinates.map(inner => {
return Array.isArray(inner) ? inner.map(elem => parseFloat(elem)) : parseFloat(inner);
});
}
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}
The above is actually a JavaScript Object. 上面实际上是一个JavaScript对象。
{"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[["-102.038635","36.800488"],["-94.567716","36.800488"],["-96.149793","43.381098"],["-104.060178","43.508721"]]}}}
where as the above is actually a JSON. 上面实际上是一个JSON。
As one of the answer suggested, whenever you send data to server, everything in the payload will automatically be converted to strings. 作为建议的答案之一,每当您将数据发送到服务器时,有效负载中的所有内容都会自动转换为字符串。 You should cast the values as numbers. 您应该将值转换为数字。
Also, I don't think any parse would be able to do it and you should write your own code for that. 另外,我认为任何解析都无法做到,您应该为此编写自己的代码。 The reason I say it is because, the parser would never know if the original value was a string sent by the client or was actually a number since all the keys and values will become strings. 我之所以说这是因为,解析器永远不会知道原始值是客户端发送的字符串还是实际上是数字,因为所有键和值都将变成字符串。
Assum I have 假设我有
var payload = {"geoJSON":{"type":"Feature","geometry":{"type":"LineString","coordinates":[[-102.038635,36.800488],[-94.567716,36.800488],[-96.149793,43.381098],[-104.060178,43.508721]]}}}
Using JSON.stringify(payload) to stringify this before send server. 在发送服务器之前,使用JSON.stringify(payload)对此字符串进行字符串化。
payload = JSON.stringify(payload)
// after this code then sending payload to server
In the server using JSON.parse(payload) to use this 在服务器上使用JSON.parse(payload)来使用
// server nodejs
var receivedContent = JSON.parse(payload)
// then you can using receivedContent with format you want
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.