错误域= NSCocoaErrorDomain代码= 3840 JSONObject

时间:2019-11-18 16:28:50

标签: php ios json swift xcode

我正在尝试使用xcode的switf 4来使用其余的Web服务,这是我的代码:

doFilter

这是我在xcode控制台中的答案:

  

Linea 50:错误域= NSCocoaErrorDomain代码= 3840“无效值   字符0附近。“ UserInfo = {NSDebugDescription =无效的值   字符0。} Linea 52:可选(“ | {\” login \“:\” true \“}”)

这是我的代码Web服务:

func login_crm(user:String, passwd:String) {


    let url = URL(string: "https://intranet.jytcolombia.com/ws/ws_ios/iosws_login.php?par1=\(user)&par2=\(passwd)")
    let request = URLRequest(url: url!)
    let task = URLSession.shared.dataTask(with: request){data, response, error in
        guard let data = data else{
            print("Linea 33 \(error)")
            return
        }
        do{
            print("Recibimos respuesta")
            if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: String] {
                DispatchQueue.main.async {
                    let result = json["login"]

                    if(result == "true"){

                        let result = json["login"]

                        if(result == "true"){



                        } else {
                            let alert = UIAlertController(title: "Inicio de Sesion", message: "Usuario o contraseña invalidos", preferredStyle: .alert)
                            alert.addAction(UIAlertAction(title: "OK", style: .default))
                            self.present(alert, animated: true, completion: nil)
                        }

                    } else {
                        let alert = UIAlertController(title: "Inicio de Sesion", message: "Usuario o contraseña invalidos", preferredStyle: .alert)
                        alert.addAction(UIAlertAction(title: "OK", style: .default))
                        self.present(alert, animated: true, completion: nil)
                    }

                }
            }

        }catch let parseError{
            print("Linea 50: \(parseError)")
            let responseString = String(data: data, encoding: .utf8)
            print("Linea 52: \(responseString)")
        }
    }
    task.resume()



}

1 个答案:

答案 0 :(得分:1)

我可以看到您的响应字符串包含|。字符:

"|{\"login\":\"true\"}"

这会导致json解析问题。请检查您的服务器代码。