我只想从此JSON响应中解析userID <- hash key
total_connected <- Integer (Integer incremented on a connection from the same user id)
status <- "online"/"offline" (If the total_connected is 0 then offline otherwise online)
怎么办
secure = true
transports = WebSocket
reconnection = true
forceNew = true
我正在使用alamofire
答案 0 :(得分:0)
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
print(response) //to get status code
if let status = response.response?.statusCode {
switch(status){
case 201:
print("example success")
default:
print("error with response status: \(status)")
}
} //to get JSON return value
if let result = response.result.value {
let JSON = result as! NSDictionary
print(JSON)
}
}
答案 1 :(得分:0)
您可以使用此 JSON 生成器:Link
import Foundation
struct RootClass: Codable {
let message: String
let result: Result
let status: Bool
private enum CodingKeys: String, CodingKey {
case message = "message"
case result = "result"
case status = "status"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
message = try values.decode(String.self, forKey: .message)
result = try values.decode(Result.self, forKey: .result)
status = try values.decode(Bool.self, forKey: .status)
}
}
struct Result: Codable {
let address: String
let cityName: String
let countryName: String
let createdAt: String
let deviceType: Int
let firstName: String
let gender: Int
let id: Int
let isActive: Int
let lastName: String
let otp: Int
let pincode: String
let stateName: String
let swithAddWithoutHub: Int
let updatedAt: String
let userEmail: String
let userMobile: Int
let userName: String
let userPassword: String
let verified: Int
private enum CodingKeys: String, CodingKey {
case address = "address"
case cityName = "city_name"
case countryName = "country_name"
case createdAt = "created_at"
case deviceType = "device_type"
case firstName = "first_name"
case gender = "gender"
case id = "id"
case isActive = "is_active"
case lastName = "last_name"
case otp = "otp"
case pincode = "pincode"
case stateName = "state_name"
case swithAddWithoutHub = "swith_add_without_hub"
case updatedAt = "updated_at"
case userEmail = "user_email"
case userMobile = "user_mobile"
case userName = "user_name"
case userPassword = "user_password"
case verified = "verified"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
address = try values.decode(String.self, forKey: .address)
cityName = try values.decode(String.self, forKey: .cityName)
countryName = try values.decode(String.self, forKey: .countryName)
createdAt = try values.decode(String.self, forKey: .createdAt)
deviceType = try values.decode(Int.self, forKey: .deviceType)
firstName = try values.decode(String.self, forKey: .firstName)
gender = try values.decode(Int.self, forKey: .gender)
id = try values.decode(Int.self, forKey: .id)
isActive = try values.decode(Int.self, forKey: .isActive)
lastName = try values.decode(String.self, forKey: .lastName)
otp = try values.decode(Int.self, forKey: .otp)
pincode = try values.decode(String.self, forKey: .pincode)
stateName = try values.decode(String.self, forKey: .stateName)
swithAddWithoutHub = try values.decode(Int.self, forKey: .swithAddWithoutHub)
updatedAt = try values.decode(String.self, forKey: .updatedAt)
userEmail = try values.decode(String.self, forKey: .userEmail)
userMobile = try values.decode(Int.self, forKey: .userMobile)
userName = try values.decode(String.self, forKey: .userName)
userPassword = try values.decode(String.self, forKey: .userPassword)
verified = try values.decode(Int.self, forKey: .verified)
}
}
然后用alamofire解析
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseJSON { response in
print(response) //to get status code
if let status = response.response?.statusCode {
switch(status){
case 201:
print("example success")
default:
print("error with response status: \(status)")
}
} //to get JSON return value
if let result = response.result.value {
let JSON = result as! RootClass
print(JSON)
}
}
答案 2 :(得分:-1)
我的建议是使用SwiftyJSON: SwiftyJSON
看这个例子:
Alamofire.request(url, method: .get).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
print("JSON: \(json)")
case .failure(let error):
print(error)
}
}
之后,您可以说类似这样的话
json["id"]