使用Alamofire和SwiftyJson解析数据

问题描述 投票:0回答:2

我的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
            }
        ],

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

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

如何使用swiftyjson打印值或“url”

json swift alamofire swifty-json
2个回答
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
投票

使用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
}
© www.soinside.com 2019 - 2024. All rights reserved.