json - 我对 JSONSerialization.jsonObject(with: dataResponse, options: .allowFragments) 有一些疑问
问题描述
- 我有一个问题
let jsonResponse = try JSONSerialization.jsonObject (with: dataResponse, options: .allowFragments)
print ("jsonResponse ==> (jsonResponse)")
我想更改值 jsonResponse ==> jsonArray 但该值未发送到 jsonArray jsonArray ==> jsonDictionary
然后使用 JSON 字典检查登录...
- 我有更多错误
guard let jsonDictionary: Dictionary = jsonArray [0] else {==> Have warning = Non-optional expression of type '[String: Any]' used in a check for optionals
return
} // guard
print ("jsonDictionary ==> (jsonDictionary)")
如果我改变
- JSONSerialization.jsonObject (with: dataResponse, options: [])
出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项。”
// function check Authen
func checkAuthen(truckNo: String, pass: String, imei: String) -> Void {
let myconstant = Myconstant()
let urlcheckLogin = myconstant.jsonGetDataCheckAuthen(pTruckNo: truckNo, pPassword: pass, pIMEI: imei)
print("urlcheckLogin ==> \(urlcheckLogin)")
guard let url = URL(string: urlcheckLogin) else {
return
}//Guard
// task
let task = URLSession.shared.dataTask(with: url){ (data, response, error) in
guard let dataResponse = data, error == nil else{
print("Have Error")
return
}//guard
do{
// read json from API
let jsonResponse = try JSONSerialization.jsonObject(with: dataResponse, options: .allowFragments)
print("jsonResponse ==> \(jsonResponse)")
// change json to array
guard let jsonArray = jsonResponse as? [[String:Any]] else{
return
}//guard
print("jsonArray ==> \(jsonArray)")
guard let jsonDictionary: Dictionary = jsonArray[0] else{
return
}//guard
print("jsonDictionary ==>\(jsonDictionary)")
// check true password for json dictionary
let truePassword: String = jsonDictionary["Password"] as! String
print("truePassword ==>\(truePassword)")
if pass == truePassword{
// password incorrect
DispatchQueue.main.async {
self.performSegue(withIdentifier: "GotoPlanData", sender: self)
}//DispatchQueue
}else{
// password incorrect
self.showAlert(title: "Password incorrect", message: "Plase try again")
}//if
}catch let myerror{
print(myerror)
// check display username in database
print("No have user \(truckNo) in database")
DispatchQueue.main.async {
self.showAlert(title: "No username", message: "No have user \(truckNo) in database")
}//DispatchQueue
}//catch
}//task
task.resume()
}//checkAuthen
结果
No Space
urlcheckLogin ==> http://www.testservice.com/Service/CheckUserLogin/60-7625/1234/355750067867310
username ==> Optional("60-7625")
password ==> Optional("1234")
jsonResponse ==> [{ "Result": "Valid", "TruckNo": "60-7625", "TruckID": "10" }]
解决方案
好的,我想我知道“JSON text did to start ....”的原因是由于在返回的dataResponse中有一个标记\,导致无法读取jsonResponse值
Example:
dataResponse ==> "[ { \"Result\": \"Valid\", \"TruckNo\": \"60-7625\", \"TruckID\": \"10\" }]"
我想切记。我应该如何编写代码?
正在寻找替换 Swift String 中的字符。
Example :
dataResponse ==> "[ { \"Result\": \"Valid\", \"TruckNo\": \"60-7625\", \"TruckID\": \"10\" }]"
我想用“”替换如何获得:
Example:
dataResponse ==> "[{" Result ":" Valid "," TruckNo ":" 60-7625 "," TruckID ":" 10 "}]"
我怎样才能做到这一点?
推荐阅读
- hibernate - QueryDSL CaseBuilder 不适用于 java.util.Date
- javascript - 如何在 OpenLayers 中重叠或相同几何体的情况下选择哪个要素叠加
- python - 使用 python-chess 从 PGN 文件中提取移动作为字符串
- reactjs - 部署在 Heroku 上的 ExpressJS API 出现 500 内部服务器错误
- excel - 类型不匹配 - 范围和数字
- javascript - 当对象是 JSON 对象与 Array 对象时,为什么 [For.. in] 循环中使用的变量返回不同的值?
- python - 在 python 中复制 F# map join
- c# - Unity C# 函数 Application.CanStreamedLevelBeLoaded(String string) 有什么作用?
- python - index() 函数不起作用,较长的函数也无法找到包含最大/最小数的行号
- c - CMake:未评估目标属性中的生成器表达式