在快速Web服务器中访问JSON值

我能够使用此答案中的代码访问发布到服务器的JSONstring中的值。

如果服务器获得{"MyKey":"My Value"}那么可以使用request.body.MyKey访问"MyKey"的值。

但是发送到我的服务器的JSONstring如下所示:

 [{"id":"1","name":"Aaa"},{"id":"2","name":"Bbb"}] 

我无法find一个方法来访问任何东西。 你怎么做呢?

request.body是一个标准的JavaScript对象,在你的情况下是一个香草的JavaScript数组。 你会像处理任何JavaScript Array对象一样处理request.body 。 例如

 app.post('/', function(request, response){ var users = request.body; console.log(users.length); // the length of the array var firstUser = users[0]; // access first element in array console.log(firstUser.name); // log the name users.forEach(function(item) { console.log(item) }); // iterate the array logging each item ...