Parse Array of JSON objects in NodeJS

后端 未结 3 760
自闭症患者
自闭症患者 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:12

    You may actually send the JSON directly to server.

        $.ajax({
            url: "/echo",
            type: 'POST',
            data: JSON.stringify(QuestionsArray),
            processData: false,
            contentType: 'application/json'
        }).success(function (data) {
            alert(data);
        });
    

    And in node.js, use bodyParser.json to get it back.

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

    By the way, do not use Array as variable name because Array represent the Array class used by JavaScript and your code has overwritten it.

提交回复
热议问题