我正在从API中获取JSON
数据,并且成功获得了响应,但是对我来说,有一个问题,即如何在没有密钥的情况下获得直接价值,让我向您展示我的响应
回复
{
"message": "Notification Sent Successfully.",
"success": "1",
"message_list": [
{
"+919722552298": [
{
"body": "Sent from your Twilio trial account - This is test message by purvesh",
"from": "",
"sid": "",
"status": "sent",
"date_updated": "Mon, 01 Jul 2019 12:58:49 +0000",
"date_sent": "Mon, 01 Jul 2019 12:58:49 +0000",
"date_created": "Mon, 01 Jul 2019 12:58:49 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is test message by purvesh",
"from": "",
"sid": "",
"status": "sent",
"date_updated": "Sat, 29 Jun 2019 09:36:07 +0000",
"date_sent": "Sat, 29 Jun 2019 09:36:07 +0000",
"date_created": "Sat, 29 Jun 2019 09:36:07 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is test message by purvesh",
"from": "",
"sid": "",
"status": "sent",
"date_updated": "Sat, 29 Jun 2019 09:35:18 +0000",
"date_sent": "Sat, 29 Jun 2019 09:35:18 +0000",
"date_created": "Sat, 29 Jun 2019 09:35:18 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is a test message...",
"from": "",
"sid": "",
"status": "sent",
"date_updated": "Sat, 29 Jun 2019 05:56:43 +0000",
"date_sent": "Sat, 29 Jun 2019 05:56:43 +0000",
"date_created": "Sat, 29 Jun 2019 05:56:43 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is a test...",
"from": "",
"sid": "",
"status": "sent",
"date_sent": "Thu, 27 Jun 2019 11:52:44 +0000",
"date_created": "Thu, 27 Jun 2019 11:52:43 +0000",
"date_updated": "Thu, 27 Jun 2019 11:52:44 +0000",
"to": ""
}
]
},
{
"+61459036405": [
{
"body": "Sent from your Twilio trial account - Thanks for the message. Configure your number's SMS URL to change this message.Reply HELP for help.Reply STOP to unsubscribe.Msg&Data rates may apply.",
"from": "",
"sid": "",
"status": "delivered",
"date_sent": "Sat, 29 Jun 2019 06:25:33 +0000",
"date_created": "Sat, 29 Jun 2019 06:25:33 +0000",
"date_updated": "Sat, 29 Jun 2019 06:25:35 +0000",
"to": ""
},
{
"body": "Hello Ron ",
"from": "",
"sid": "",
"status": "received",
"date_sent": "Sat, 29 Jun 2019 06:25:33 +0000",
"date_created": "Sat, 29 Jun 2019 06:25:33 +0000",
"date_updated": "Sat, 29 Jun 2019 06:25:33 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is a test message...",
"from": "",
"sid": "",
"date_updated": "Sat, 29 Jun 2019 06:08:42 +0000",
"date_created": "Sat, 29 Jun 2019 06:08:42 +0000",
"status": "sent",
"date_sent": "Sat, 29 Jun 2019 06:08:42 +0000",
"to": ""
}
]
},
{
"+918733009842": [
{
"body": "Sent from your Twilio trial account - This is a test...",
"from": "",
"sid": "",
"date_updated": "Thu, 27 Jun 2019 12:32:45 +0000",
"date_created": "Thu, 27 Jun 2019 12:32:45 +0000",
"status": "sent",
"date_sent": "Thu, 27 Jun 2019 12:32:45 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is a test...",
"from": "",
"sid": "",
"status": "sent",
"date_created": "Thu, 27 Jun 2019 12:25:37 +0000",
"date_updated": "Thu, 27 Jun 2019 12:25:37 +0000",
"date_sent": "Thu, 27 Jun 2019 12:25:37 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is a test...",
"from": "",
"sid": "",
"status": "sent",
"date_created": "Thu, 27 Jun 2019 12:07:04 +0000",
"date_updated": "Thu, 27 Jun 2019 12:07:04 +0000",
"date_sent": "Thu, 27 Jun 2019 12:07:04 +0000",
"to": ""
},
{
"body": "Sent from your Twilio trial account - This is a test...",
"from": "",
"sid": "",
"status": "sent",
"date_created": "Thu, 27 Jun 2019 12:01:55 +0000",
"date_updated": "Thu, 27 Jun 2019 12:01:55 +0000",
"date_sent": "Thu, 27 Jun 2019 12:01:55 +0000",
"to": ""
}
]
}
]
}
这是我的回复,现在我将向您展示我正在成功获取message_list
数组的数据,但问题是如何获取电话号码(即您在我的回复中显示的主要数组),我希望数字列表是数组在辅助消息列表数组中
func messageListAPI(){
let preferences = UserDefaults.standard
let uid = "u_id"
let acTkn = "acc_tkn"
let u_ID = preferences.object(forKey: uid)
let A_Token = preferences.object(forKey: acTkn)
let params = ["user_id": u_ID!,"access_token": A_Token!,"twilio_number": "+13344313181"]
// print(params)
Alamofire.request(messagelist, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
switch response.result{
case.success(let value):
let json = JSON(value)
print(json)
let message = json["message"].stringValue
let success = json["success"].stringValue
let data = json["message_list"]
let model = ResponseModel(message: message, success: success, messageList: data) // Here i am getting error
case.failure(let error):
print(error.localizedDescription)
}
})
}
这是我成功获得message_list
的API调用方法,谁能帮助我获取数字列表
答案 0 :(得分:3)
您应创建以下模型以进行响应:
/// Main response model
struct ResponseModel: Codable {
let message: String
let success: String
let messageList: [[String: [MessageList]]]
enum CodingKeys: String, CodingKey {
case message, success
case messageList = "message_list"
}
}
/// MessageList Model
struct MessageList: Codable {
let body: String
let from: String
let sid: String
let status: Status
let dateSent: String
let dateCreated
let dateUpdated
let to: String
enum CodingKeys: String, CodingKey {
case body, from, sid, status
case dateSent = "date_sent"
case dateCreated = "date_created"
case dateUpdated = "date_updated"
case to
}
}
/// For status field
enum Status: String, Codable {
case delivered = "delivered"
case received = "received"
case sent = "sent"
}
您可以将这些模型与 Alamofire
一起使用/// YOU MUST CHANGE 'ResponseType' with the real type of response
func messageListAPI(_ completion: @escaping (ResponseType) -> Void) {
let urlString = "http://yourUrl"
Alamofire.request(urlString).response { response in
guard let data = response.data else { return }
do {
let decoder = JSONDecoder()
let response: ResponseType = try decoder.decode(ResponseModel.self, from: data)
let numberList = response.messageList.compactMap({ $0.keys })
print("my numberList -> \(numberList)")
completion(response)
} catch let error {
print(error)
completion(nil)
}
}
}
用法:
messageListAPI { response in
/// If response is nil it's means error appear.
/// Otherwise it is successful
print(response)
}
您还可以使用 URLSession
guard let yourUrl = URL(string: "https://myUrl") else { return }
URLSession.shared.dataTask(with: yourUrl) { (data, response , error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
let data = try decoder.decode(ResponseModel.self, from: data)
print(data)
} catch let error {
print("Err", error)
}
}.resume()
您可以使用这些模型来解析响应JSON
。然后,您将获得这样的号码列表:
let numberList = ResponseModel.messageList.compactMap({ $0.keys })
如果对将Codable模型用于API响应有任何疑问,请立即询问。
享受。
答案 1 :(得分:0)
只需粘贴您的json响应,即可从https://app.quicktype.io/
获得模型URLSession.shared.dataTask(with: yourURL) { (data, resonse, err) in
if err == nil{
}else{
do{
let model = JSONDecoder().decode(YourModel.self, from: data!)
}catch(let decodeErr){
print(decodeErr)
}
}
}.resume()
答案 2 :(得分:-1)
由于消息列表是一个数组,因此您可以尝试执行此操作-
let data = json["message_list"]
for (index, item) in data.enumerated() {
print(item[index])
}