Javascript 在 NodeJS 中解析 JSON 对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28707836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse Array of JSON objects in NodeJS
提问by Apha
I am wondering how can I parse Array of JSON objects in NodeJS?
我想知道如何解析 NodeJS 中的 JSON 对象数组?
I want to post JSON array to the server, and be able to use the received array as a regualar JavaScript array.
我想将 JSON 数组发布到服务器,并且能够将接收到的数组用作常规 JavaScript 数组。
Thanks in advance.
提前致谢。
This is my front-end part that I am converting Array to String using stringify function
这是我的前端部分,我使用 stringify 函数将数组转换为字符串
document.getElementById("sendJson").addEventListener("click", function () {
$.post("/echo", JSON.stringify(QuestionsArray), function (data) {
alert(data);
});
})
This my back-end part that I am trying to convert Array of JSON object to Array
这是我的后端部分,我试图将 JSON 对象数组转换为数组
app.post('/echo', function (req, res) {
var Array = JSON.parse(JSON.stringify(req.toString()));
res.end(Array[0]["QuestionText"].toString());
});
This is Array that I am trying to sent to the server:
这是我试图发送到服务器的数组:
[
{
"QuestionText":"What is your Name",
"QuestionType":1
},
{
"QuestionText":"Where are you from",
"QuestionType":2,
"ChoiceList":[
"US",
"UK"
]
},
{
"QuestionText":"Are you married",
"QuestionType":3,
"ChoiceList":[
"Yes",
"No"
]
}
]
采纳答案by Vsevolod Goloviznin
In your app.js:
在您的app.js:
var bodyParser = require("body-parser");
...
app.use(bodyParser.urlencoded({extended: true}));
Then you can just use req.bodyto get the posted values:
然后你可以使用req.body来获取发布的值:
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);
});
回答by cfs
I'll try to explain this. First of all, you are crating a json string on the client.
我会尽力解释这一点。首先,您正在客户端上创建一个 json 字符串。
JSON.stringify(QuestionsArray)
Then on the server, you are doing the same again:
然后在服务器上,您再次执行相同的操作:
JSON.stringify(req.toString()) // this is not needed
Then you parse the double stringifyed json string to a javascript object:
然后将双字符串化的 json 字符串解析为 javascript 对象:
JSON.parse(JSON.stringify(req.toString()))
So now you actually have to parse it twice :). If you just stringify it on the server as you are now, and just call:
所以现在你实际上必须解析它两次:)。如果您像现在一样在服务器上对其进行字符串化,只需调用:
var arr = JSON.parse(req.toString());
You will get a javascript object that you can access like this:
您将获得一个可以像这样访问的 javascript 对象:
res.end(arr[0].QuestionText.toString());
Have a look at this jsFiddle and open your developer tools. Look at the console when it runs and you will see where the problem is: example
看看这个 jsFiddle 并打开你的开发者工具。运行时查看控制台,就会发现问题出在哪里:example
回答by Alex Lau
You may actually send the JSON directly to server.
您实际上可以将 JSON 直接发送到服务器。
$.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.jsonto get it back.
在 node.js 中,使用bodyParser.json它来取回它。
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 Arrayas variable name because Arrayrepresent the Arrayclass used by JavaScript and your code has overwritten it.
顺便说一句,不要Array用作变量名,因为Array代表ArrayJavaScript 使用的类并且您的代码已经覆盖了它。

