这是我需要发布的JSON:
{
"results": [
{
"case_id": 1,
"status_id": 5,
"comment": "This test failed"
},
{
"case_id": 2,
"status_id": 1,
"comment": "This test passed",
},
..
{
"case_id": 1,
"assignedto_id": 5,
"comment": "Assigned this test to Joe"
}
..
]
}
我尝试做的是:
let parameters = [
"results" : data
] as [String : Any]
let postData = try JSONSerialization.data(withJSONObject: parameters, options: [])
这里,data是一个类型结构数组(data:[param]):
struct param {
var status_id: Int
var case_id: String
var comment: String
}
JSONSerialization的代码失败。它进入catch()块。我尝试过这样的参数并且有效,
let parameters = [ "results" : [
[
"case_id": "20275",
"status_id": 5,
"comment": "This test failed"
],
[
"case_id": "20276",
"status_id": 1,
"comment": "This test passed",
],
]] as [String : Any]
如何重现此结构?因为我无法对状态ID和案例ID的值进行硬编码。我将状态ID和case id存储在数组中。我想创建一个结构数组并替换id值就足够了,不幸的是它不能正常工作。我如何发布数据?
答案 0 :(得分:1)
'param'应该是一个字典,不能用struct。
请求正文数据类型为:class StoreBussiness extends FormRequest
{
public function rules()
{
$rules = [];
if ($this->type === 'a') {
}
return rules;
}
}