我能够使用 this answer 中的代码访问发布到服务器的 JSON 字符串中的值。
如果服务器获取{"MyKey":"My Value"}
,则可以使用request.body.MyKey
访问"MyKey"
的值。
但是发送到我的服务器的 JSON 字符串如下所示:
[{"id":"1","name":"Aaa"},{"id":"2","name":"Bbb"}]
我找不到访问其中任何内容的方法。你是怎么做到的?
最佳答案
request.body
是一个标准的 JavaScript 对象,在您的例子中是一个普通的 JavaScript 数组。您只需像处理任何 JavaScript 一样处理 request.body
Array
目的。例如
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
...
关于json - 访问 Express Web 服务器中的 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10020884/