Javascript 解析节点js中的JSON字符串?

Javascript 解析节点js中的JSON字符串?,javascript,json,node.js,Javascript,Json,Node.js,我试图发送一个JSON字符串,并在节点js的服务器端解析它。 我想提取像title这样的特定值,但由于某些原因,当我试图解析它时,我得到了未定义的值 这就是我到目前为止所尝试的 Home.ejs $.each(item, function(key, value) { var s = JSON.stringify(value); s = s.replace(/^\s+|\s+$/g,'');

我试图发送一个JSON字符串,并在节点js的服务器端解析它。 我想提取像title这样的特定值,但由于某些原因,当我试图解析它时,我得到了未定义的值

这就是我到目前为止所尝试的

Home.ejs

 $.each(item, function(key, value)
             {
                 var s = JSON.stringify(value);
                 s = s.replace(/^\s+|\s+$/g,'');
                 info.push(s);
             });
             console.log("info",info);

            if (response.status == 'ok') {
                $.ajax({
                    type: 'POST',
                    url: '/home',
                    data: JSON.stringify(info),
                    success: function (data) {


                    },
                    error: function (data) {
                        console.log('error');
                        console.log(data)
                    }
                });
            }
这是它在控制台中打印出来的内容。 信息

server.js

app.post("/home",function(req,res)
{

    var data = (req.body);



    console.log("name",data);

    var news_title = [];
    var news_link =[];
    var news_date =[];
    /*for(var i in req.body){

        var item = req.body.items[i];
        //console.log(item);

    }*/
    //console.log("name",name);
});
这是它在控制台中打印出来的内容

name { '["{\"title\":\"Trump ends self-made crisis\",\"pubDate\":\"2017-06-22 22:36:00\",\"link\":\"http://www.cnn.com/2017/06/22/politics/trump-comey-tapes-recordings-white-house/index.html\",\"guid\":\"http://www.cnn.com/2017/06/22/politics/trump-comey-tapes-recordings-white-house/index.html\",\"author\":\"\",\"thumbnail\":\"\",\"description\":\"President Donald Trump finally, grudgingly, had no choice but to come clean.\",\"content\":\"President Donald Trump finally, grudgingly, had no choice but to come clean.\",\"enclosure\":{\"link\":\"http://i2.cdn.turner.com/cnnnext/dam/assets/170622175804-trump-tweet-restricted-super-169.jpg\"},\"categories\":': [ '' ] }
name { '["{\"title\":\"Trump ends self-made crisis\",\"pubDate\":\"2017-06-22 22:36:00\",\"link\":\"http://www.cnn.com/2017/06/22/politics/trump-comey-tapes-recordings-white-house/index.html\",\"guid\":\"http://www.cnn.com/2017/06/22/politics/trump-comey-tapes-recordings-white-house/index.html\",\"author\":\"\",\"thumbnail\":\"\",\"description\":\"President Donald Trump finally, grudgingly, had no choice but to come clean.\",\"content\":\"President Donald Trump finally, grudgingly, had no choice but to come clean.\",\"enclosure\":{\"link\":\"http://i2.cdn.turner.com/cnnnext/dam/assets/170622175804-trump-tweet-restricted-super-169.jpg\"},\"categories\":': [ '' ] }

我想提取title值,但由于某些原因,我在尝试req.body.title时一直没有定义。

看起来您遇到了问题,因为您在每个数组元素上使用了
JSON.stringify
,然后在编译的数组上使用
JSON.stringify

您应该通过串接整个数组而不串接每个部分来获得预期的结果。i、 e.在您的情况下,只需将

              $.ajax({
                    type: 'POST',
                    url: '/home',
                    data: JSON.stringify(item),
                    success: function (data) {


                    }

你是否在你的节点应用程序中使用了body解析器?正如Peter Grainger所说,你有一袋芯片,每个芯片都是单独包装的。你打开袋子,拿出一块薯片,咬一口——那只是saran的包味。。。这是您的
未定义的
。不要把东西包两次。只需确保你有袋子(
JSON.stringify(info)
),但不要包装单个芯片(
JSON.stringify(value)
->
value
)。另外,不要在
JSON上执行
replace
。stringify
result-
JSON。stringify
已经尽可能紧凑了,几乎没有什么好结果。由于缩进不良,您无法看到它,但
在程序中的该点不可用。执行
info。在循环中推送(项目)
(不使用stringify),然后stringify
info
。是否需要执行
$。如果只是发送项目数组,则每个
循环?这将发送大量消息,数组中的每个项对应一条消息
              $.ajax({
                    type: 'POST',
                    url: '/home',
                    data: JSON.stringify(item),
                    success: function (data) {


                    }