简体   繁体   English

如何将x-www-form-urlencoded有效负载转换为json

[英]how to convert x-www-form-urlencoded payload to json

i am using the npm request module to post data. 我正在使用npm 请求模块发布数据。 by default the posy is of x-www-form-urlencoded so on the server side i am receiving the data like this 默认情况下,posy是x-www-form-urlencoded因此在服务器端,我正在接收这样的数据

{
  'files[0][path]': 'myfile1',
  'files[0][contents]': 'some content',
  'files[0][active]': 'true'
}

how do i parse the data to json? 如何将数据解析为json? like 喜欢

{
  files: [{
    path: 'mypath',
    content: 'mycontent',
    active: true
  } {
    path: 'mypath2',
    content: 'mycontent2',
    active: false
  }]
}

I think express js might use a middle wire for this, but I am using hapi server. 我认为Express js可能为此使用中间线,但是我正在使用hapi服务器。 what are my options to parse the payload to json? 将有效负载解析为json的选项是什么?

There are a number of existing node packages that deal with parsing requests. 有许多现有的节点程序包可以处理解析请求。 Check out https://www.npmjs.com/package/body-parser-json . 查看https://www.npmjs.com/package/body-parser-json To write a solution from scratch would just be a waste of time. 从头开始编写解决方案只会浪费时间。

Have you looked into jQuery's serializeArray() api? 您是否研究过jQuery的serializeArray()API? This might be good enough depending on your use case but here's another library built on top of serializeArray(). 这可能已经足够好了,具体取决于您的用例,但这是 serializeArray()之上构建的另一个库

在服务器端和hapijs上使用bodyparsing,我碰巧碰到了做到这一点的hapi-bodyparser

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

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