gpt4 book ai didi

json - 我有一些关于 JSONSerialization.jsonObject 的问题(带有 : dataResponse, 选项 : . allowedFragments)

转载 作者:行者123 更新时间:2023-11-30 10:42:41 24 4
gpt4 key购买 nike

  1. 我有一个关于
  2. 的问题
let jsonResponse = try JSONSerialization.jsonObject (with: dataResponse, options: .allowFragments)
print ("jsonResponse ==> (jsonResponse)")

我想改变这个值jsonResponse ==> jsonArray 但值没有发送到 jsonArrayjsonArray ==> 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 字符串的形式发送,显式用双引号引起来,这是非常不寻常的。

    如果数据以双引号开头,则必须删除第一个和最后一个字节

    let trimmedData : Data 
    if dataResponse.first == 0x22 {
    trimmedData = dataResponse.dropFirst().dropLast()
    } else {
    trimmedData = dataResponse
    }
    guard let jsonResponse = try JSONSerialization.jsonObject(with: trimmedData) as? [[String:Any]] else { return }
    print("jsonResponse ==> \(jsonResponse)")

    关于json - 我有一些关于 JSONSerialization.jsonObject 的问题(带有 : dataResponse, 选项 : . allowedFragments),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56454146/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com