我想遍历times
,但是调试时times
是undefined
。
router.post('/something', function (req, res) {
var times = req.body.times;
for (i = 0; i < times.length; i++) {
// do something
}
}
这是调试时body.times的样子:
{"duration":3600,"startTime":540,"weekDay":0},{"duration":3600,"startTime":600,"weekDay":0},{"duration":3600,"startTime":660,"weekDay":0},{"duration":3600,"startTime":1080,"weekDay":6}:""
这是VSCode调试中的屏幕截图。将spans
替换为times
。 (为清楚起见,我刚刚将其更改为times
。)
以下是快速代码:
var jsonArray: [[String: Any]] = [/*The array*/]
let json: [String: Any] = ["times": jsonArray]
guard let body = (try? JSONSerialization.data(withJSONObject: json,
options: []))
else {
return nil
}
这是我配置快递的方式:
var app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(bodyParser.json({ type: 'application/*+json' }))
app.use(bodyParser.json({ type: 'application/json' }))
app.use(bodyParser.json({ type: 'application/x-www-form-urlencoded'}))
json的输出:
(lldb) po print(json)
["spans": [["weekDay": 0, "duration": 3600.0, "startTime": 540], ["weekDay": 0, "duration": 3600.0, "startTime": 600]]]
答案 0 :(得分:0)
代码中有两个要测试的部分
1.Node js
通过邮寄测试您的api 检查内容类型(应用程序/ json或多部分) 并验证其是否有效
2)检查您的Swift代码并与服务器的内容类型匹配
您的数据
{"duration":3600,"startTime":540,"weekDay":0},{"duration":3600,"startTime":600,"weekDay":0},{"duration":3600,"startTime":660,"weekDay":0},{"duration":3600,"startTime":1080,"weekDay":6}:""
您要遍历对象而不是数组 所以它不会起作用。