如何使用Alamofire从Xcode中的API的JSON获取特定对象?
我正在Swift中进行电子商务应用程序的项目,并且必须从我使用mockapi.io创建的API中获得数据,
API的链接为:
[
{
"categorie1": "Fruits",
"categorie2": "Animals",
"categorie3": "Juices",
"categorie4": "Vegetables",
"categorie5": "Alcohol",
"categorie6": "Desserts",
"id": "1"
}
]
/cancorias“ rel =” nofollow noreferrer> https://62858a2ff0e8f0bb7c057f14.mockapi.io/categorias 但是, 响应ws“包含从API获取数据的结构,看起来像这样:
import Foundation
struct ResponseWS:Decodable{
let Datos:[Categories]
}
struct Categories:Decodable{
let id: String
let categorie: String
}
我还有一个名为“ connectws”的文件,我在其中使用Alamofire处理数据:
import Foundation
import Alamofire
class ConnectWS{
static let cmd = ConnectWS()
private let urlBase = "https://62858a2ff0e8f0bb7c057f14.mockapi.io/categorias"
private let code = 200...299
func getUsers(user: String, pass: String,success: @escaping(_ user: String) -> (), failure: @escaping(_ error: Error?)-> ()) {
AF.request(urlBase,method: .get).validate(statusCode: code).responseDecodable(of: ResponseWS.self){
response in
if let respuesta = response.data {
print(String(decoding: respuesta, as: UTF8.self))
success(String(decoding: respuesta, as: UTF8.self))
}else{
print(response.error!)
failure(response.error)
}
}
}
}
最后我有了我的ViewController,我想显示数据API:
import UIKit
class ViewControllerCategorias: UIViewController {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var message: UILabel!
@IBOutlet weak var buttonOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonAction(_ sender: Any) {
ConnectWS.cmd.getUsers(user: "erik", pass: "1234", success: {user in
self.name.text = user
}, failure: {error in
self.message.text = error.debugDescription
})
}
}
它会从API中返回所有数据,而我想做的就是从API中获取特定的“类别”。
如您在“ viewController”中看到的,我有一个按钮,当我单击它时, 按钮,标签显示:
[{"categorie1":"Fruits","categorie2":"Animals","categorie3":"Juices","categorie4":"Vegetables","categorie5":"Alcohol","categorie6":"Desserts","id":"1"}]
如何获得API的特定对象?
I am doing a project of an E-commerce app in swift and the data must be obtained from an API that I created with mockapi.io
The link of the API is: https://62858a2ff0e8f0bb7c057f14.mockapi.io/categorias but if you don't want to enter the link here is how the JSON looks like:
[
{
"categorie1": "Fruits",
"categorie2": "Animals",
"categorie3": "Juices",
"categorie4": "Vegetables",
"categorie5": "Alcohol",
"categorie6": "Desserts",
"id": "1"
}
]
I have a file named "ResponseWS" that contains the structs to obtain the data from the API, it looks like this:
import Foundation
struct ResponseWS:Decodable{
let Datos:[Categories]
}
struct Categories:Decodable{
let id: String
let categorie: String
}
I also have a file named "ConnectWS" where I process the data with the Alamofire:
import Foundation
import Alamofire
class ConnectWS{
static let cmd = ConnectWS()
private let urlBase = "https://62858a2ff0e8f0bb7c057f14.mockapi.io/categorias"
private let code = 200...299
func getUsers(user: String, pass: String,success: @escaping(_ user: String) -> (), failure: @escaping(_ error: Error?)-> ()) {
AF.request(urlBase,method: .get).validate(statusCode: code).responseDecodable(of: ResponseWS.self){
response in
if let respuesta = response.data {
print(String(decoding: respuesta, as: UTF8.self))
success(String(decoding: respuesta, as: UTF8.self))
}else{
print(response.error!)
failure(response.error)
}
}
}
}
And finally I have my ViewController where I want to show the data of the API:
import UIKit
class ViewControllerCategorias: UIViewController {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var message: UILabel!
@IBOutlet weak var buttonOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonAction(_ sender: Any) {
ConnectWS.cmd.getUsers(user: "erik", pass: "1234", success: {user in
self.name.text = user
}, failure: {error in
self.message.text = error.debugDescription
})
}
}
As you can see in the "ViewController" I have a button that when I click it, it returns ALL the data from the API, and what I want to do is get especifics "categories" from the API
When I click the button, the label shows:
[{"categorie1":"Fruits","categorie2":"Animals","categorie3":"Juices","categorie4":"Vegetables","categorie5":"Alcohol","categorie6":"Desserts","id":"1"}]
How can I get a specific object of the API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将响应数据解码为该对象。您可以使用此通用方法进行解码。只需创建一个模型类/结构,您要在解码JSON后使用该模型/结构。
You have to decode the response data into that object. You can use this generic method for decoding. Just create a model class/struct that you want to use for converting after decoding JSON.