swift 如何使用Alamofire解析这个JSON并追加到Array中[已关闭]

p5cysglq  于 2023-02-28  发布在  Swift
关注(0)|答案(1)|浏览(120)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
这是我的JSON响应-

{
    "status": true,
    "message": "Sub category lists",
    "category": \[
        {
            "cat_sr": "1",
            "cat_id": "C329847",
            "cat_name": "Grocery",
            "cat_image": "uploads/category/C329847/Grocery store.jpg",
            "created_at": "-000001-11-29T18:06:32.000000Z",
            "updated_at": "2022-12-17T10:18:46.000000Z"
        },
        {
            "cat_sr": "3",
            "cat_id": "C3U4TIL",
            "cat_name": "Educational services",
            "cat_image": "uploads/category/C3U4TIL/Educational services.jpg",
            "created_at": "2022-12-17T10:18:27.000000Z",
            "updated_at": "2022-12-17T10:18:27.000000Z"
        },
        {
            "cat_sr": "4",
            "cat_id": "C5WP1HI",
            "cat_name": "Sports & Fitness",
            "cat_image": "uploads/category/C5WP1HI/app_logo (4).png",
            "created_at": "2022-12-17T10:20:44.000000Z",
            "updated_at": "2023-01-24T05:13:28.000000Z"
        },
        {
            "cat_sr": "5",
            "cat_id": "CDBLXS5",
            "cat_name": "Hardware & Sanitary",
            "cat_image": "",
            "created_at": "2022-12-17T10:20:58.000000Z",
            "updated_at": "2022-12-17T10:20:58.000000Z"
        },
        {
            "cat_sr": "6",
            "cat_id": "CBAFISB",
            "cat_name": "Individual services",
            "cat_image": "",
            "created_at": "2022-12-17T10:21:12.000000Z",
            "updated_at": "2022-12-17T10:21:12.000000Z"
        },
        {
            "cat_sr": "7",
            "cat_id": "C3ISLGZ",
            "cat_name": "Electrical",
            "cat_image": "",
            "created_at": "2022-12-17T10:21:25.000000Z",
            "updated_at": "2022-12-17T10:21:25.000000Z"
        },
        {
            "cat_sr": "8",
            "cat_id": "CW16AJQ",
            "cat_name": "Electronics",
            "cat_image": "",
            "created_at": "2022-12-17T10:21:37.000000Z",
            "updated_at": "2022-12-17T10:21:37.000000Z"
        },
        {
            "cat_sr": "9",
            "cat_id": "CSDB9M0",
            "cat_name": "Clothing",
            "cat_image": "",
            "created_at": "2022-12-17T10:21:58.000000Z",
            "updated_at": "2022-12-17T10:21:58.000000Z"
        },
        {
            "cat_sr": "10",
            "cat_id": "CUIW0U6",
            "cat_name": "Household items",
            "cat_image": "",
            "created_at": "2022-12-17T10:22:09.000000Z",
            "updated_at": "2022-12-17T10:22:09.000000Z"
        },
        {
            "cat_sr": "11",
            "cat_id": "C4C8AEJ",
            "cat_name": "Healthcare and diagnostics",
            "cat_image": "",
            "created_at": "2022-12-17T10:22:21.000000Z",
            "updated_at": "2022-12-17T10:22:21.000000Z"
        },
        {
            "cat_sr": "12",
            "cat_id": "C4YKVX3",
            "cat_name": "Pharmaceutical",
            "cat_image": "",
            "created_at": "2022-12-17T10:22:32.000000Z",
            "updated_at": "2022-12-17T10:22:32.000000Z"
        },
        {
            "cat_sr": "13",
            "cat_id": "C81HDST",
            "cat_name": "Automotive & Cycle",
            "cat_image": "",
            "created_at": "2022-12-17T10:23:30.000000Z",
            "updated_at": "2022-12-17T10:23:30.000000Z"
        },
        {
            "cat_sr": "14",
            "cat_id": "CTH9I5U",
            "cat_name": "Cold Storage",
            "cat_image": "",
            "created_at": "2022-12-17T10:23:40.000000Z",
            "updated_at": "2022-12-17T10:23:40.000000Z"
        },
        {
            "cat_sr": "15",
            "cat_id": "COBJOC4",
            "cat_name": "Food",
            "cat_image": "",
            "created_at": "2022-12-17T10:24:13.000000Z",
            "updated_at": "2022-12-17T10:24:13.000000Z"
        }
    \]
}

这是我的模特课。

struct UserModel: Codable {
        let status: Bool
        let message: String
        let category: [Category]
    }
    
    struct Category: Codable {
        let catSr, catID, catName, catImage: String
        let createdAt, updatedAt: String
    
        enum CodingKeys: String, CodingKey {
            case catSr = "cat_sr"
            case catID = "cat_id"
            case catName = "cat_name"
            case catImage = "cat_image"
            case createdAt = "created_at"
            case updatedAt = "updated_at"
        }

这是我的控制器类。在这个类中我用alamofire解析了json,并想从分类中获取数据。

import UIKit
import Alamofire

class AllUserTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var allEmailArray:[String] = []
    var allAddressArray:[String] = []
    var allNameArray:[String] = []
    
    
    @IBOutlet var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        print(allNameArray.count)
        
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 44
        tableView.frame = CGRectMake(0, 0, 320, 568)
       getJsonData()
    }
    // MARK: - Table view data source
    func getJsonData() {
        let urlFile = "http://test.tribyssapps.com/api/allUsers"
        AF.request(urlFile,method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil).response { response in
            switch response.result {
            case .success(let data):
                do {
                    let jsonData = try JSONDecoder().decode(UserModel.self, from: data!)
                    //Closure calling
//                    print(jsonData.response)
//                    let allEmail = jsonData.response.first?.email as? String
                    jsonData.response.forEach { data in
                        let nameData = data.name as String?
                        let emailData = data.email as String?
                        let fullAddressData = data.address as String?
                        self.allEmailArray.append(emailData!)              =
                        print(self.allEmailArray)
                        self.allNameArray.append(nameData!)
                        self.allAddressArray.append(fullAddressData!)
                        
                    }
//                    self.allEmailArray.append(allEmail!)
//                    print(self.allEmailArray.count)
                } catch {
                    print(error.localizedDescription)
                }
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
wn9m85ua

wn9m85ua1#

查询的结构不正确。建议您使用此站点app.quicktype.io

struct YourStruct: Codable {
        let status: Bool
        let message: String
        let category: [Category]
    }
    
    struct Category: Codable {
        let catSr, catID, catName, catImage: String
        let createdAt, updatedAt: String
    
        enum CodingKeys: String, CodingKey {
            case catSr = "cat_sr"
            case catID = "cat_id"
            case catName = "cat_name"
            case catImage = "cat_image"
            case createdAt = "created_at"
            case updatedAt = "updated_at"
        }
    }

相关问题