如何访问nodejs中的数组对象?

时间:2021-01-31 07:01:01

I am getting this req in my req.body . I want to parse the details .Below is my req.

我在我的req.body中得到了这个请求。我想解析细节.Below是我的要求。

"[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"

I want output like

我想输出像

number:

cmdb_ci:

category:

How do i parse this array object in nodejs. Please help

我如何解析nodejs中的这个数组对象。请帮忙

3 个解决方案

#1


1  

Use JSON.parse() like this:

像这样使用JSON.parse():

var aJsonArrString = "[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"

var aObjList = JSON.parse(aJsonArrString);
for(var i = 0; i < aObjList.length; i++) {
    console.log('number   : ' + aObjList[i].number);
    console.log('cmdb_ci  : ' + aObjList[i].cmdb_ci);
    console.log('category : ' + aObjList[i].category); 
}

#2


1  

You Can Use

您可以使用

JSON.parse(req.body);

#3


0  

This looks like JSON, I don't know if the escaping \ are coming from your way of logging the value or something so I'll expect it is a valid string to start off.

这看起来像JSON,我不知道转义\是否来自你记录值或某事的方式,所以我期望它是一个有效的字符串开始。

You can use

您可以使用

var my_list = JSON.parse(req.body);
//Access like any other array...
my_list[0].number;

#1


1  

Use JSON.parse() like this:

像这样使用JSON.parse():

var aJsonArrString = "[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"

var aObjList = JSON.parse(aJsonArrString);
for(var i = 0; i < aObjList.length; i++) {
    console.log('number   : ' + aObjList[i].number);
    console.log('cmdb_ci  : ' + aObjList[i].cmdb_ci);
    console.log('category : ' + aObjList[i].category); 
}

#2


1  

You Can Use

您可以使用

JSON.parse(req.body);

#3


0  

This looks like JSON, I don't know if the escaping \ are coming from your way of logging the value or something so I'll expect it is a valid string to start off.

这看起来像JSON,我不知道转义\是否来自你记录值或某事的方式,所以我期望它是一个有效的字符串开始。

You can use

您可以使用

var my_list = JSON.parse(req.body);
//Access like any other array...
my_list[0].number;