Parse Array of JSON objects in NodeJS

后端 未结 3 751
自闭症患者
自闭症患者 2020-12-09 12:50

I am wondering how can I parse Array of JSON objects in NodeJS?

I want to post JSON array to the server, and be able to use the received array as a regualar JavaScri

3条回答
  •  眼角桃花
    2020-12-09 13:13

    In your app.js:

    var bodyParser = require("body-parser");
    ...
    app.use(bodyParser.urlencoded({extended: true}));
    

    Then you can just use req.body to get the posted values:

    app.post('/echo', function (req, res) {
        var Array = req.body.data;
        res.end(Array[0]["QuestionText"].toString());
    });
    

    In front-end, don't do any stringifying:

    $.post("/echo", {data: QuestionsArray}, function (data) {
        alert(data);
    });
    

提交回复
热议问题