json 如何将Dictionnary数据字符串存储到表中[已关闭]

flseospp  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(101)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

昨天关门了。
Improve this question
在我的swift上,我从json中的php收到了一个数据。
当我打印它显示所有直接我想知道是否有一种方法把这些数据直接在一个表中或如果我必须删除字符我不想和削减我的字符串等。
这是我代码

let parameters = ["id":"\(id)","password":"\(password)"]
        
        var sParams = ""
        
        for (key,value) in parameters{
            sParams += key + "=" + (value as String) + "&"
            print("\(key),\(value as String)")

        }
        if !sParams.isEmpty{
            sParams = "?" + sParams
          
            if sParams.hasSuffix("&"){
                sParams.removeLast()
               
            }
            
            urlLink = urlLink + sParams
        }else{
            print("!sParams.isEmpty fail")
        }

        let serializer = DataResponseSerializer(emptyResponseCodes: [200,204,205])
        
        var request = URLRequest(url : URL(string:urlLink)!)
        AF.request(request).uploadProgress{progress in }.response(responseSerializer: serializer){ response in
            if response.error == nil {
                var responseString: String!
                
                responseString = ""
                
                if response.data != nil {
                    responseString = String(bytes:response.data!, encoding: .utf8)
                    
                    
                }else{
                    responseString = response.response?.description
                    
                    
                }
                var dataJson = responseString ?? ""
                
                dataJson = dataJson.replacingOccurrences(of: "\"", with: "")
                dataJson = dataJson.replacingOccurrences(of: "password:", with: "")
                dataJson = dataJson.replacingOccurrences(of: "email:", with: "")
                dataJson = dataJson.replacingOccurrences(of: "{", with: "")
                dataJson = dataJson.replacingOccurrences(of: "}", with: "")
                dataJson = dataJson.replacingOccurrences(of: "[", with: "")
                dataJson = dataJson.replacingOccurrences(of: "]", with: "")
                print(dataJson)
            

                
                var finalData = dataJson.components(separatedBy: ",")
                
                if finalData[0] == id as String  && finalData[1] == password as String{
                    self.present(HomeClientViewController(), animated: true)
                }else{
                    print("erreur dans la conversion")
                }

              //  print(responseString ?? "")
               // print(response.response?.statusCode as Any)
                var data : NSData!
                
                if let data = data {
                    //Succeed
                    let dataLength = data.length
                    print("Size: \(dataLength) Bytes")
                }else{
                    //print(data)
                }
               
                print("response time: \(response.metrics?.taskInterval.duration ?? 0)")
                    //self.present(HomeClientViewController(), animated: true)
            }
        }

下面是我如何接收数据的日志:

[{"email":"myEmail@gmail.com","password":"1234"}]
f4t66c6m

f4t66c6m1#

谢谢你所有的答案,所以我尝试使用一个解码这里是我用来正确解码我的JsonData的代码:

struct Login: Decodable {
                    let email: String
                    let password: String
                }
            
                let jsonObjectData = dataJson!.data(using: .utf8)!
                let login = try? JSONDecoder().decode(
                    Login.self,
                    from: jsonObjectData
                )
                
                print(login!.email as Any)
                print(login!.password as Any)
vfhzx4xs

vfhzx4xs2#

首先,解码json:

struct User: Decodable {
    let email: String
    let password: String
}

let jsonFormatter = JSONDecoder()
let jsonData = Data(jsonString.utf8)

do {
    let user = try jsonFormatter.decode([User].self, from: jsonData).first
    // Then, use user item to populate the view you prefer.
} catch {
    // Handle exception
}

相关问题