我尝试通过 WCF 服务从 sql 服务器获取数据
I am try to fetch the data from sql server through WCF service
import UIKit
public struct student: Decodable {
let id:Int
let name:String
}
class ViewController: UIViewController {
@IBAction func btnshow(_ sender: Any) {
let link = "http://10.211.55.3/WcfService1/Service1.svc/GetData"
guard let url = URL(string: link)else{
print("error during connection")
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data
else{ print("there is no data")
return
}
do{
let students = try JSONDecoder().decode(student.self, from: data)
print(students)
} catch{
print("conversion error")
print(error)
}
}.resume()
}
}
keyNotFound(CodingKeys(stringValue: "id", intValue: nil),
Swift.DecodingError.Context(codingPath: [], debugDescription: "No
value associated with key CodingKeys(stringValue: \"id\", intValue:
nil) (\"id\").", underlyingError: nil))
好吧,打印出来的错误很容易解释。您获得的数据未按您期望的方式格式化,并且缺少某些键(id
键),因此 JSONDecoder()
无法对其进行解码。
确保您收到的响应格式正确,然后再尝试对其进行解码。
编辑:所以在看到您的评论后,您的模型与您收到的 JSON 不匹配,它应该是这样的:
import UIKit
public struct JsonStudent: Decodable {
let students: [Student]
enum CodingKeys: String, CodingKey {
case students = "GetDataResult"
}
}
public struct Student: Decodable {
let id: Int
let name: String
}
class ViewController: UIViewController {
@IBAction func btnshow(_ sender: Any) {
let link = "http://10.211.55.3/WcfService1/Service1.svc/GetData"
guard let url = URL(string: link) else {
print("error during connection")
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data
else { print("there is no data")
return
}
do {
let students = try JSONDecoder().decode(JsonStudent.self, from: data)
print(students)
} catch {
print("conversion error")
print(error)
}
}.resume()
}
}
import UIKit
public struct student: Decodable {
let id:Int
let name:String
}
class ViewController: UIViewController {
@IBAction func btnshow(_ sender: Any) {
let link = "http://10.211.55.3/WcfService1/Service1.svc/GetData"
guard let url = URL(string: link)else{
print("error during connection")
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data
else{ print("there is no data")
return
}
do{
let students = try JSONDecoder().decode(student.self, from: data)
print(students)
} catch{
print("conversion error")
print(error)
}
}.resume()
}
}
keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"id\", intValue: nil) (\"id\").", underlyingError: nil))
好吧,打印出来的错误很容易解释。您获得的数据未按您期望的方式格式化,并且缺少某些键(id
键),因此 JSONDecoder()
无法对其进行解码。
确保您收到的响应格式正确,然后再尝试对其进行解码。
编辑:所以在看到您的评论后,您的模型与您收到的 JSON 不匹配,它应该是这样的:
import UIKit
public struct JsonStudent: Decodable {
let students: [Student]
enum CodingKeys: String, CodingKey {
case students = "GetDataResult"
}
}
public struct Student: Decodable {
let id: Int
let name: String
}
class ViewController: UIViewController {
@IBAction func btnshow(_ sender: Any) {
let link = "http://10.211.55.3/WcfService1/Service1.svc/GetData"
guard let url = URL(string: link) else {
print("error during connection")
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data
else { print("there is no data")
return
}
do {
let students = try JSONDecoder().decode(JsonStudent.self, from: data)
print(students)
} catch {
print("conversion error")
print(error)
}
}.resume()
}
}