在获取JSON值时发出SWIFT Alamofire
我从服务器得到响应,并将其转换为NSDictionary在获取JSON值时发出SWIFT Alamofire,swift,alamofire,swift4,Swift,Alamofire,Swift4,我从服务器得到响应,并将其转换为NSDictionary Alamofire.request(URL, method: .post, parameters: parameters).responseJSON { response in if let result = response.result.value { let jsonData = result as! NSDictionary
Alamofire.request(URL, method: .post, parameters: parameters).responseJSON
{
response in
if let result = response.result.value {
let jsonData = result as! NSDictionary
let recordJSON=jsonData.value(forKey: "records") as! NSDictionary
let result = recordJSON.value(forKey: "result") as! NSArray
print(result)
}
}
现在在结果中值为
(
{
imagePath = "/SERVER/api/upload/geRsB.jpeg";
propertyId = 11;
userId = 5;
},
{
imagePath = "/SERVER/api/upload/RebJC.jpeg";
propertyId = 14;
userId = 5;
},
{
imagePath = "/SERVER/api/upload/fuM3F.jpeg";
propertyId = 18;
userId = 5;
}
)
那么,现在我如何从结果的每个索引(imagePath、propertyId、userId)中获得更多的值呢
提前谢谢。我建议使用
可解码的
,但您可以试试
if let result = response.result.value as? [String:Any] {
if let recordJSON = result["records"] as? String:Any] {
if let result = recordJSON["result"] as? [Any] {
print(result)
for item in result {
if let inner = item as? [String:Any] {
print(inner["imagePath"])
print(inner["propertyId"])
print(inner["userId"])
}
}
}
}
}
您必须使用Codable或decodable来解析响应: 1:这是响应数据的结构 2:然后使用此结构解析数据: 3:然后您可以使用以下解析第一个对象结果:
for data in result {
if let innerData = data as? [String:Any] {
let imagePath = innerData["imagePath"]
let propertyId = innerData["propertyId"]
let userId = innerData["userId"]
}
}
或者您可以直接使用,无需解析,如下所示:
for data in result {
if let innerData = data as? [String:Any] {
let imagePath = innerData["imagePath"]
let propertyId = innerData["propertyId"]
let userId = innerData["userId"]
}
}
如果你的回答是这样的
[
{
"imagePath": "/SERVER/api/upload/geRsB.jpeg",
"propertyId": 11,
"userId": 5
},
{
"imagePath": "/SERVER/api/upload/geRsB.jpeg",
"propertyId": 11,
"userId": 5
}
]
import Foundation
typealias Users = [UserElement]
struct UserElement: Codable {
let imagePath: String
let propertyID, userID: Int
enum CodingKeys: String, CodingKey {
case imagePath
case propertyID = "propertyId"
case userID = "userId"
}
}
// MARK: Convenience initializers
extension UserElement {
init(data: Data) throws {
self = try JSONDecoder().decode(UserElement.self, from: data)
}
}
extension Array where Element == Users.Element {
init(user data: Data) throws {
self = try JSONDecoder().decode(Users.self, from: data)
}
}
您可以像这样使用可编码协议
[
{
"imagePath": "/SERVER/api/upload/geRsB.jpeg",
"propertyId": 11,
"userId": 5
},
{
"imagePath": "/SERVER/api/upload/geRsB.jpeg",
"propertyId": 11,
"userId": 5
}
]
import Foundation
typealias Users = [UserElement]
struct UserElement: Codable {
let imagePath: String
let propertyID, userID: Int
enum CodingKeys: String, CodingKey {
case imagePath
case propertyID = "propertyId"
case userID = "userId"
}
}
// MARK: Convenience initializers
extension UserElement {
init(data: Data) throws {
self = try JSONDecoder().decode(UserElement.self, from: data)
}
}
extension Array where Element == Users.Element {
init(user data: Data) throws {
self = try JSONDecoder().decode(Users.self, from: data)
}
}
然后在请求中这样使用
Alamofire.request(URL, method: .post, parameters: parameters).response
{
response in
guard let data = response.data else {return}
let users = try? Array.init(user:data)
}
不要在Swift中使用
NSDictionary
和/或NSArray
进行JSON解析。阅读Decodable
上的文档来解析JSON。要做到这一点,返回的JSON必须与OP的问题类似,但实际上,OP在捕获数组之前并没有解析它,因此您应该这样做,以便result NSArray不响应。result.value是一个字典