Разбор данных с использованием Alamofire и SwiftyJson - PullRequest
0 голосов
/ 29 апреля 2019

My 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

Как я могу напечатать значение или "URL", используя swiftyjson

Ответы [ 2 ]

4 голосов
/ 29 апреля 2019

Вы можете использовать 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 голос
/ 01 мая 2019

Анализ данных JSON с использованием SwiftyJSON

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
}
...