Node.js正文解析器不会在json中用方括号解析输入名称

时间:2015-02-02 17:04:28

标签: json node.js parsing express body-parser

我在Node.js

中遇到Express正文解析器的问题

我有这样的输入:

<input id="personEmail1" type="email" name="person[0][email]" placeholder="Email1" class="form-control">
<input id="personEmail2" type="email" name="person[1][email]" placeholder="Email2" class="form-control">

当我提交表单后,我在console.log中得到了这个:

{ 'person[0][email]': 'info@sss.sss', 'person[1][email]': 'info@ggg.ggg' }

但是我希望它以json格式解析:

{ person: [{email: 'info@sss.sss'}, {email: 'info@ggg.ggg'}] }

我做错了什么?

4 个答案:

答案 0 :(得分:5)

对于Express版本4.x,当您需要手动安装body-parser和multer时,您希望从帖子中获取嵌套属性,例如data[test]形式为req.body.data.test,您需要设置:

app.use(bodyParser.urlencoded({ extended: true }));

答案 1 :(得分:1)

您正在使用Express正文解析器中间件括号表示法。但是,作为可以做什么的一个例子......

使用此视图:

<form method="post">
    <label for="person_email_1">Email address 1</label>
    <input id="person_email_1" name="person[0][email]" type="email" value="email1@example.com"> <br>

    <label for="person_email_2">Email address 2</label>
    <input id="person_email_2" name="person[1][email]" type="email" value="email2@example.com"> <br>

    <button type="submit">Submit v1</button>
</form>
<br>
<form method="post">
    <label for="person_email_1">Email address 1</label>
    <input id="person_email_1" name="person[email][0]" type="email" value="email1@example.com"> <br>

    <label for="person_email_2">Email address 2</label>
    <input id="person_email_2" name="person[email][1]" type="email" value="email2@example.com"> <br>

    <button type="submit">Submit v2a</button>
</form>
<br>
<form method="post">
    <label for="person_email_1">Email address 1</label>
    <input id="person_email_1" name="person[email]" type="email" value="email1@example.com"> <br>

    <label for="person_email_2">Email address 2</label>
    <input id="person_email_2" name="person[email]" type="email" value="email2@example.com"> <br>

    <button type="submit">Submit v2b</button>
</form>
<br>
<form method="post">
    <label for="person_email_1_address">Email address 1</label>
    <input id="person_email_1_address" name="person[emailAddresses][0][address]" type="email" value="email1@example.com">
    <input id="person_email_1_description" name="person[emailAddresses][0][description]" type="text" value="lorem ipsum 1"> <br>

    <label for="person_email_2_address">Email address 2</label>
    <input id="person_email_2_address" name="person[emailAddresses][1][address]" type="email" value="email2@example.com">
    <input id="person_email_2_description" name="person[emailAddresses][1][description]" type="text" value="lorem ipsum 2"> <br>

    <button type="submit">Submit v3</button>
</form>

...和这个帖子处理程序:

function postHandler(req, res) {
    console.log(JSON.stringify(req.body)); // show in console
    res.send(req.body); // show in browser
}

版本1(您的版本,适用于我,并返回您想要的结果)req.body:

{
  "person": [
    {"email": "email1@example.com"},
    {"email": "email2@example.com"}
  ]
}

版本2a&amp; 2b(一个字符串数组,有/没有索引号)req.body:

{
  "person": {
    "email": [
      "email1@example.com",
      "email2@example.com"
    ]
  }
}

版本3(对象数组)req.body:

{
  "person": {
    "emailAddresses": [
      {
        "address": "email1@example.com",
        "description": "lorem ipsum 1"
      },
      {
        "address": "email2@example.com",
        "description": "lorem ipsum 2"
      }
    ]
  }
}

我亲自使用过版本2&amp; 3在节点/ Express / jquery / Bootstrap业务应用程序中,个人或企业帐户可以拥有无​​限的电话号码,电子邮件地址和URL。身体解析器括号表示法使其变得愚蠢。

答案 2 :(得分:1)

即使你已经为你的密钥使用了有效的Javascript语法,它们仍然只是字符串而没有 JSON解析器会尝试在它们上面调用eval

但是,JSON已经有了一个数组概念,它应该按照你期望的方式工作。

向我们展示调用console.log的代码会很有用。但我猜想,您需要重新考虑字段名称的命名约定。

<input id="personEmail1" type="email" name="personEmail1" placeholder="Email1" class="form-control">
<input id="personEmail2" type="email" name="personEmail2" placeholder="Email2" class="form-control">

然后从该数据创建Javascript对象。

function handler(req, res) {
   var people = [
     { email: req.body.personEmail1 },
     { email: req.body.personEmail2 }
   ];

   console.log(people[0]); // person 1
   console.log(people[1]); // person 2
   console.log(people);    // both people
}

答案 3 :(得分:0)

我使用此https://github.com/macek/jquery-serialize-object将表单序列化为JSON。它可以根据需要解析数据,并且易于使用。

    $.ajax({
      type: 'post',
      data: $form.serializeJSON(),
      contentType: 'application/json',
      url: $url,
      success: function (result) { ... }
    });

我认为这是最简单的