json - 访问 Express Web 服务器中的 JSON 值

标签 json web-services parsing node.js express

我能够使用 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/

相关文章:

java - 使用 android volley 库发送 JSONArray POST 请求

python - 为什么 Python json.dumps 在混合 utf-8 和 unicode 字符串上失败?

json - 从 Golang 调试 JSON 错误

c# - 如何使用 WSDL 文件在 Visual Studio.NET 中创建 Web 服务?

java - 更改 JAVA Axis Web 服务响应 XML 正文

ios - 来自照片 UITableView 的 json 不显示

php - MediaWiki API - 'opensearch' & 'query' 和 API 调用 url 中的 'generator' & 'list' 有什么区别

web-services - Gradle项目构建失败

ios - 如何获取 XML 元素并存储它? objective-c

android - 将来自 Web 服务的图像保存在 SQLite 数据库中