如何在Node Express中接收JSON对象字符串

时间:2016-11-23 05:37:39

标签: json node.js express polymer

我正在使用Polymer <iron-ajax>JSON.stringify数据发布到Node Express服务器,但该JSON字符串转换为我无法解析的奇怪格式(如下所示)。我想解析email值。

<iron-ajax>正文内容设置为:

this.$.ajax.body = JSON.stringify({
  "email": "pyaephyoeshein@gmail.com",
  "full_name": "Pyae Phyoe Shein",
  "phone": "123456",
  "file":{},
  "question1_answer": "answer one",
  "question2_answer": "answer two",
  "question3_answer": "answer three",
  "question1": "What is question one?",
  "question2": "What is question two?",
  "question3": "What is question three?"
  })

Node Express收到:

{"{\"email\":\"pyaephyoeshein@gmail.com\",\"full_name\":\"Pyae Phyoe Shein\",\"phone\":\"123456\",\"file\":{},\"question1_answer\":\"answer one\",\"question2_answer\":\"answer two\",\"question3_answer\":\"answer three\",\"question1\":\"What is question one?\",\"question2\":\"What is question two?\",\"question3\":\"What is question three?\"}":""}

4 个答案:

答案 0 :(得分:2)

您不需要JSON.stringify() <iron-ajax>.body的内容,因为这会导致正文to be sent as a string with its Content-Type set to application/x-www-form-urlencoded(而不是application/json),要求您转换为JSON服务器

<iron-ajax>.body设置为预期的object<iron-ajax>.contentType设置为application/json,Express服务器应自动将其反序列化为JSON对象。

<iron-ajax url="http://httpbin.org/put"
           method="put"
           content-type="application/json"
           last-response="{{response}}"></iron-ajax>

HTMLImports.whenReady(() => {
  Polymer({
    is: 'x-foo',
    properties: {
      response: {
        type: Object,
        observer: '_responseChanged'
      }
    },
    ready: function() {
      this.$.ajax.body = {
        email: "pyaephyoeshein@gmail.com",
        full_name: "Pyae Phyoe Shein",
        phone: "123456",
        file: {},
        question1_answer: "answer one",
        question2_answer: "answer two",
        question3_answer: "answer three",
        question1: "What is question one?",
        question2: "What is question two?",
        question3: "What is question three?"
      };
      this.$.ajax.generateRequest();
    },
    _responseChanged: function(response) {
      console.log('response', response);
    }
  });
});
<head>
  <base href="https://polygit.org/polymer+1.7.0/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-ajax/iron-ajax.html">
</head>
<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <iron-ajax url="http://httpbin.org/put"
                 method="put"
                 id="ajax"
                 content-type="application/json"
                 last-response="{{response}}"></iron-ajax>
    </template>
  </dom-module>
</body>

codepen

答案 1 :(得分:0)

您需要使用body-parser来解析POST请求的JSON正文。

npm install body-parser --save

然后在你的快递应用中。

var express = require('express');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.json());

然后,您可以作为JSON对象访问正文。

app.post('/whatever', function(req, res) {
    console.log(req.body.email);
});

答案 2 :(得分:0)

您有两个功能:JSON.parseJSON.stringifyJSON.parse将字符串转换为JSON对象,而JSON.stringify将JSON对象转换为字符串。

您正在做的是字符串化已经是字符串的内容,从而产生结果。

试试这个:

  blabla = JSON.parse('{"email": "pyaephyoeshein@gmail.com",   \
                        "full_name": "Pyae Phyoe Shein",       \
                        "phone": "123456",                     \
                        "file":{},                             \
                        "question1_answer": "answer one",      \
                        "question2_answer": "answer two",      \
                        "question3_answer": "answer three",    \
                        "question1": "What is question one?",  \
                        "question2": "What is question two?",  \
                        "question3": "What is question three?" \
                        }') ;

  console.log(JSON.stringify(blabla,4)) ;

注意:

(1)4告诉方法在将JSON转换为字符串时将JSON层次结构中的每个级别缩进4个空格。

(2)需要\来分割多个行中的字符串。

希望这能让你更清楚。

答案 3 :(得分:0)

我相信它在数据对象上做了两次JSON.stringify,所以你不应该自己对数据对象进行字符串化。

尝试,

this.$.ajax.body = {
  "email": "pyaephyoeshein@gmail.com",
  "full_name": "Pyae Phyoe Shein",
  "phone": "123456",
  "file":{},
  "question1_answer": "answer one",
  "question2_answer": "answer two",
  "question3_answer": "answer three",
  "question1": "What is question one?",
  "question2": "What is question two?",
  "question3": "What is question three?"
  };

你应该得到,

{"email":"pyaephyoeshein@gmail.com","full_name":"Pyae Phyoe Shein","phone":"123456","file":{},"question1_answer":"answer one","question2_answer":"answer two","question3_answer":"answer three","question1":"What is question one?","question2":"What is question two?","question3":"What is question three?"}