{
"values":[
[1,1,7,"Azuan Child","Anak Azuan","12345","ACTIVE","Morning",7,12,"2017-11-09 19:45:00"],
[28,1,0,"Azuan Child2","Amran","123456","ACTIVE","Evening",1,29,"2017-11-09 19:45:00"]
]
}
好的,这是我从服务器收到的JSON格式。
现在我想将其解码为我的结构体,但仍然没有成功。
struct ChildrenTable: Decodable {
var values: [[String]]?
}
我的URLSession上的调用方法是这样的:
URLSession.shared.dataTask(with: request) { (data, response, err) in
guard let data = data else { return }
let dataAsString = String(data: data, encoding: .utf8)
print(dataAsString)
do {
let children = try
JSONDecoder().decode(ChildrenTable.self, from: data)
print (children)
} catch let jsonErr {
print ("Error serializing json: ", jsonErr)
}
}.resume()
And the error that i got are
Error serializing json:
typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [Vito_Parent.ChildrenTable.(CodingKeys in _1B826CD7D9609504747BED0EC0B7D3B5).values, Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 0", intValue: Optional(0)),
Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 0", intValue: Optional(0))],
debugDescription: "Expected to decode String but found a number instead.", underlyingError: nil))
我知道数组中有一个整数,而我只对值进行字符串转换
var values: [[String]]?
(这就是为什么会出现此错误的原因),但由于它遵循Decodable协议,我不能在我的结构体中使用任何多维数组或元组。我也不能将数据转换为字典,因为它会抛出错误“预计解码字典,但找到了数组”。
你有解决这个问题的想法吗?我尝试将数据强制转换为字符串类型,但仍然没有运气...
附言:如果所有的JSON格式都是字符串类型,那么就不会有问题,但我没有更改它的权限,因为我从API调用它。