【发布时间】:2017-01-27 20:53:06
【问题描述】:
我现在尝试了很多东西,但仍然得到: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON 写入中的类型无效 (_SwiftValue)”
更新: 我扫描条形码并保存信息:
let calendar = Calendar.current
let expiryDate = (calendar as NSCalendar).date(byAdding: .month, value: 3, to: Date(), options: [])?.description
let barcode = BarcodeData(barcodeValue: value,
datetime: dateTime,
expiryDate: expiryDate!,
latitude: latitude.description,
longitude: longitude.description,
status: txtStatus.text!,
type: txtType.text!,
extraText: "")
然后将对象映射到JSON字符串,似乎斜线(/)是由这个函数添加的:
let jsonBarcode = Mapper<BarcodeData>().toJSONString(barcode)
然后将条形码添加到字符串列表中:
barcodeDataList.append(jsonBarcode)
当我单击一个按钮时,我会调用 Web 服务,该服务以以下形式预期参数:
let testParams : Parameters =
[ "udid": "my_udid",
"data": jsonArray
]
jsonArray 由 BarcodeData 对象的数组组成,如上所示。
与 Web 服务的通信如下所示:
Alamofire.request(url, method: .post, parameters: testParams, encoding: JSONEncoding.default).validate().responseJSON { response in
switch response.result {
case .success:
print("Validation successful")
if let json = response.result.value {
print("JSON: \(json)")
}
case .failure(let error):
print("Error: \(error)")
}
}
以下内容传递给ws:
["udid": "\"001-my_udid\"", "data": [
"{\"latitude\":\"0.0\",\"status\":\"Empty\",\"datetime\":\"2016-09-20 05:10\",\"longitude\":\"0.0\",\"type\":\"ABC123\",\"barcodevalue\":\"123456\"}"
]]
“数据”的 json 数组在 jsonlint.com 进行验证,来自服务器的响应采用 json 对象的形式,例如:
{result: "Data successfully received"}
【问题讨论】:
-
那些值Parameters和jsonArray是什么?
-
尝试将响应 JSON 中的 \" 替换为 IOS 端的 ",其余的都可以。
-
Abdul:正如您在我编辑的帖子中看到的那样,ObjectMappers 函数似乎正在添加斜杠 (/)。
-
据我所知,根据规范在 JSON 对象中使用斜杠是可以的。所以这不应该是这里的问题,除非 Alamofire 不支持它。这会很奇怪,因为 Alamofire 经常与 ObjectMapper 一起使用,它实际上添加了斜线符号。
标签: json swift alamofire swift3 swifty-json