简体   繁体   中英

How to encode json plain text into json data using node js

POST plain text (from another API) simulate with postman using plain text

{ "name":"brad" , "address":"mystreet" }

on Node:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json());

app.post('/api/test', function (req, res) {
    var contact = req.body; 
    console.log(contact);
}); 

Got these on the console:

{ '{\n"name":"brad",\n"address":"mystreet"\n}': '' }

How to make use or convert those text into a JSON data (formatted)?.

JSON.parse parses valid JSON text into JS objects:

 var text = '{ "name":"brad" , "address":"mystreet" }'; var json = JSON.parse(text); console.log(json); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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