【发布时间】:2015-10-14 22:17:52
【问题描述】:
当我发送一个数组时,我在服务器中检索到错误的 JSON:
客户端对象:
class Dummy : Mappable {
var test1 = "test1";
var test2 = "test2";
func mapping(map: Map) {
test1 <= map["test1"]
test2 <= map["test2"]
}
required init(){}
}
客户调用:
let wrongDummies : [Dummy] = [Dummy(), Dummy()];
let wrongDummiesJSONArray = Mapper().toJSONArray(wrongDummies)
let dummies : [String:AnyObject] = [
"right": Mapper().toJSON(Dummy()),
"again_right": ["dummy1" : Mapper().toJSON(Dummy()), "dummy2" : Mapper().toJSON(Dummy())],
"wrong": [Mapper().toJSON(Dummy()), Mapper().toJSON(Dummy())],
"again_wrong": wrongDummiesJSONArray
]
println(dummies)
request(.POST, PROFILE_URL, parameters: dummies)
客户端打印(看起来还可以):
[right: {
test1 = test1;
test2 = test2;
}, wrong: (
{
test1 = test1;
test2 = test2;
},
{
test1 = test1;
test2 = test2;
}
), again_right: {
dummy1 = {
test1 = test1;
test2 = test2;
};
dummy2 = {
test1 = test1;
test2 = test2;
};
}, again_wrong: (
{
test1 = test1;
test2 = test2;
},
{
test1 = test1;
test2 = test2;
}
)]
服务器实现(PHP):
ini_set("log_errors", 1);
ini_set("error_log", "$root/php-error.log");
error_log(print_r($_POST, true));
响应服务器:
Array
(
[again_right] => Array
(
[dummy2] => Array
(
[test2] => test2
[test1] => test1
)
[dummy1] => Array
(
[test2] => test2
[test1] => test1
)
)
[again_wrong] => Array
(
[0] => Array
(
[test2] => test2
)
[1] => Array
(
[test1] => test1
)
[2] => Array
(
[test2] => test2
)
[3] => Array
(
[test1] => test1
)
)
[right] => Array
(
[test2] => test2
[test1] => test1
)
[wrong] => Array
(
[0] => Array
(
[test2] => test2
)
[1] => Array
(
[test1] => test1
)
[2] => Array
(
[test2] => test2
)
[3] => Array
(
[test1] => test1
)
)
)
如您所见,数组中的对象按其属性的数量进行分割,而字典中的对象不会发生这种情况。
【问题讨论】: