使用Alamofire和SwiftyJson解析数据

时间:2019-04-29 12:25:40

标签: json swift alamofire swifty-json

我的 JSON -

"documents": {
        "driver": [
            {
                "id": 1,
                "name": "Driving Licence",
                "type": "DRIVER",
                "provider_document": {
                    "id": 9,
                    "provider_id": 165,
                    "document_id": "1",
                    "url": "https://boucompany.com/storage/provider/documents/b92cf551a62b6b8c183997b41b9543c6.jpeg",
                    "unique_id": null,
                    "status": "ACTIVE",
                    "expires_at": null,
                    "created_at": "2019-04-26 19:05:58",
                    "updated_at": "2019-04-27 06:37:56"
                }
            },
            {
                "id": 2,
                "name": "Bank Passbook",
                "type": "DRIVER",
                "provider_document": null
            },
            {
                "id": 3,
                "name": "Joining Form",
                "type": "DRIVER",
                "provider_document": null
            },
            {
                "id": 4,
                "name": "Work Permit",
                "type": "DRIVER",
                "provider_document": null
            },
            {
                "id": 8,
                "name": "Test Document",
                "type": "DRIVER",
                "provider_document": null
            },
            {
                "id": 9,
                "name": "NID Card",
                "type": "DRIVER",
                "provider_document": null
            },
            {
                "id": 10,
                "name": "Matrícula",
                "type": "DRIVER",
                "provider_document": null
            }
        ],

我想解析 url 名称。我在项目中使用了 Alamofire SwiftyJson 。到目前为止,我已经尝试过-

self.documentsDriver = json["documents"]["driver"][0]["provider_document"]["url"].stringValue

如何使用 swiftyjson

打印值或“ URL”

2 个答案:

答案 0 :(得分:4)

您可以使用Encodable来解析此响应,如下所示,

struct Response: Codable {
    let documents: Documents
}

struct Documents: Codable {
    let driver: [Driver]
}

struct Driver: Codable {
    let id: Int
    let name, type: String
    let providerDocument: ProviderDocument?

    enum CodingKeys: String, CodingKey {
        case id, name, type
        case providerDocument = "provider_document"
    }
}

struct ProviderDocument: Codable {
    let id, providerID: Int
    let documentID: String
    let url: String
    let status: String
    let createdAt, updatedAt: String

    enum CodingKeys: String, CodingKey {
        case id
        case providerID = "provider_id"
        case documentID = "document_id"
        case url
        case status
        case createdAt = "created_at"
        case updatedAt = "updated_at"
    }
}

要解析响应,

   let jsonData = Data() // Your API response data. 
   let response = try? JSONDecoder().decode(Response.self, from: jsonData)

   response.documents.driver.forEach { driver in
      print(driver.providerDocument?.url)
   }

答案 1 :(得分:1)

使用SwiftyJSON解析JSON数据

func convertJSONToDriverModel(json: JSON) {
    if let driver = json["documents"]["driver"].array {
        for driverJson in driver {
            let driverObj = convertToDriverJSONModel(json: driverJson)
            print(driverObj)
        }
    }
}

func convertToDriverJSONModel(json: JSON) {
    let name = json["name"].string ?? ""
    if let providerDetails = json["provider_document"].dictionary {
        let url = convertToProductDetailsJSONModel(json: JSON(providerDetails))
        print("URL is: \(url)")
    }
}

// Method to parse data inside provider_document (Here I have parsed only url)
func convertToProviderDocumentJSONModel(json: JSON) -> String {
    let url = json["url"].string ?? ""
    return url
}