简体   繁体   English

如何使用Node.js将序列化表格数据解析为json

[英]How parse serialized form data to json using Node.js

I use Angular and Node.js + Express 4 + socket.io. 我使用Angular和Node.js + Express 4 + socket.io。

I want send form to the server throw socket.io. 我想将表单发送到服务器并抛出socket.io。 I created a service in my Angular app which returns serialized form data. 我在Angular应用中创建了一个服务,该服务返回序列化的表单数据。 This service works the same way as jQuery.serialize(). 该服务的工作方式与jQuery.serialize()相同。

Then I send serialized data to the server using socket.io. 然后,我使用socket.io将序列化数据发送到服务器。 How can I parse this data to the JSON object? 如何将这些数据解析为JSON对象? I have express body-parse but I don't know how to use it not like a express middleware. 我有一个快速的body-parse,但是我不知道如何使用它而不是一个快速的中间件。

According to the jQuery docs , the jQuery.serialize() will create a query string with the form elements in the following form (example): 根据jQuery文档jQuery.serialize()将使用以下形式的表单元素创建一个查询字符串(示例):

single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1

To parse a query string into an object, you can use the querystring Node.js module: 要将查询字符串解析为对象,可以使用querystring Node.js模块:

var qs = require('querystring')
qs.parse('single=Single&multiple=Multiple&multiple=Multiple3&check=check2&radio=radio1')
// Prints:
{ single: 'Single',
  multiple: [ 'Multiple', 'Multiple3' ],
  check: 'check2',
  radio: 'radio1' }

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

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