简体   繁体   English

如何在NodeJS / JavaScript中将文本格式的JSON数组转换为JSON对象数组?

[英]How to convert text format JSON array into JSON object array in NodeJS / JavaScript?

I have a REST API created using Node.JS and ExpressJS. 我有一个使用Node.JS和ExpressJS创建的REST API。 I need to receive a JSON array from FrontEnd into my REST API. 我需要从FrontEnd接收一个JSON数组到我的REST API中。

api.post('/save_pg13_app_list', function (req, res) {
        var app_list = {
            list_object: req.body.list_object
        };
        console.log(app_list.list_object);
        res.json({ type: "success", code: 200, data: app_list.list_object });
    });

This list_object is the JSON array I'm getting from the Front End. 这个list_object是我从前端获得的JSON数组。

This is the POST endpoint for the task. 这是任务的POST端点。

http://localhost:7000/api/admin_control_manage/save_pg13_app_list HTTP://本地主机:7000 / API / admin_control_manage / save_pg13_app_list

And this is the data object I'm receiving : 这是我正在接收的数据对象:

{
 list_object :  "[{name:\"chanaka\",\"code\":10},{name:\"shashini\",code:19}]" 
}

When I'm taking this list_object in my REST API , it's a String Object. 当我在REST API中使用此list_object时,它是一个字符串对象。 Now I need to parse this to JSON. 现在,我需要将其解析为JSON。 So I used following code to convert this to JSON. 因此,我使用以下代码将其转换为JSON。

var json_array = JSON.parse(app_list.list_object);

But I'm getting this error : 但是我得到这个错误:

POST /api/admin_control_manage/save_pg13_app_list 500 0.936 ms - 1212 SyntaxError: Unexpected token n in JSON at position 2 at Object.parse (native) at /home/chanaka/WebstormProjects/PostureAPI/app/routes/admin_control.js:210:26 at Layer.handle [as handle_request] (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/layer.js:95:5) at next (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/layer.js:95:5) at /home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/index.js:277:22 at Function.process_params (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/index.js:330:12) at next (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/index.js:271:10 POST / api / admin_control_manage / save_pg13_app_list 500 0.936 ms-1212语法错误:JSON中意外标记n在/home/chanaka/WebstormProjects/PostureAPI/app/routes/admin_control.js:210:26在Object.parse(本地)位置2处在Layer.handle [作为handle_request](/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/layer.js:95:5)在下一个(/ home / chanaka / WebstormProjects / PostureAPI / node_modules / express /位于Layer.handle的Route.dispatch(/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/route.js:112:3)处的lib / router / route.js:131:13)[as handle_request] (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/layer.js:95:5)位于/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/index.js:277: 22在Function.process_params(/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/index.js:330:12)在下一个(/ home / chanaka / WebstormProjects / PostureAPI / node_modules / express / lib / router /index.js:271:10 ) at Function.handle (/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/index.js:176:3) )在Function.handle(/home/chanaka/WebstormProjects/PostureAPI/node_modules/express/lib/router/index.js:176:3)

All I need to do is, convert [{name:"chanaka",code:10},{name:"shashini",code:19}] kind of string array into JSON. 我需要做的就是将[{name:"chanaka",code:10},{name:"shashini",code:19}]类型的字符串数组转换为JSON。 What is the way to do this ? 这是怎么做的?

Try stringifying the object in the callback function; 尝试在回调函数中对对象进行字符串化;

res.json({ 
    type: "success", 
    code: 200, 
    data: JSON.stringify(app_list.list_object) 
});

JSON stringify will format it correctly so it's gets properly parsed JSON stringify将正确格式化它,以便正确解析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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