Swift4 JSONDecoderExpected可以解码Dictionary< String,Any>但是找到了一个数组 [英] Swift4 JSONDecoderExpected to decode Dictionary<String, Any> but found an array instead
问题描述
这是我的新手,但是我一直在尝试弄清楚JSONDecoder如何用于需要从MySQL数据库检索数据的登录功能,如下面的代码所示,并且正在接收此错误. /p>
快捷代码:
func testParseJson(){
var request = URLRequest(url: URL(string: "https://test.php")!)
request.httpMethod = "POST"
let postString = ("Email=test&Password=test")
print(postString)
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else { return }
do {
var responseString = String(data: data, encoding: .utf8)!
print("new response string \(responseString)")
let decoder = JSONDecoder()
let newData = try decoder.decode(User.self, from: data)
print(newData.Email)
print(newData.UserType)
} catch let err {
print("Err", err)
}
}.resume()
}
我一直在尝试使用的临时结构:
struct User: Decodable {
let U_ID: String
let Email: String
let Password: String
let UserType: String
private enum CodingKeys: String, CodingKey {
case U_ID
case Email
case Password
case UserType
}
}
并且JSON响应字符串显示为:
[{"U_ID":"1",
"Email":"test",
"Password":"test",
"UserType":"Teacher"}]
任何帮助将不胜感激.
在这里,JSON数据是Objects
的Array
.
更改
try decoder.decode(User.self, from: data)
到
try decoder.decode(Array<User>.self, from: data)
示例:
var users = [User]()
let data = """
[{"U_ID":"1","Email":"test","Password":"test","UserType":"Teacher"}]
""".data(using: .utf8)
do{
users = try JSONDecoder().decode(Array<User>.self, from: data!)
}catch{
print(error.localizedDescription)
}
print(users.first?.Email)
注意:为了更好地理解,这是我的视频系列,介绍了Swift 4中的JSON解析
I'm pretty newbie with this, but I've been attempting to work out how JSONDecoder works for a login function that needs to retrieve data from a MySQL database, as shown in my code below and am receiving this error.
Swift Code:
func testParseJson(){
var request = URLRequest(url: URL(string: "https://test.php")!)
request.httpMethod = "POST"
let postString = ("Email=test&Password=test")
print(postString)
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else { return }
do {
var responseString = String(data: data, encoding: .utf8)!
print("new response string \(responseString)")
let decoder = JSONDecoder()
let newData = try decoder.decode(User.self, from: data)
print(newData.Email)
print(newData.UserType)
} catch let err {
print("Err", err)
}
}.resume()
}
Temporary Struct I have been attempting to use:
struct User: Decodable {
let U_ID: String
let Email: String
let Password: String
let UserType: String
private enum CodingKeys: String, CodingKey {
case U_ID
case Email
case Password
case UserType
}
}
And the JSON response string reads:
[{"U_ID":"1",
"Email":"test",
"Password":"test",
"UserType":"Teacher"}]
Any help would be massively appreciated.
Here, JSON data is Array
of Objects
.
change
try decoder.decode(User.self, from: data)
to
try decoder.decode(Array<User>.self, from: data)
Example:
var users = [User]()
let data = """
[{"U_ID":"1","Email":"test","Password":"test","UserType":"Teacher"}]
""".data(using: .utf8)
do{
users = try JSONDecoder().decode(Array<User>.self, from: data!)
}catch{
print(error.localizedDescription)
}
print(users.first?.Email)
Note: For better understanding, this is my video series about JSON parsing in swift 4
这篇关于Swift4 JSONDecoderExpected可以解码Dictionary< String,Any>但是找到了一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!