Как можно распарсить массив "items" в json с помощью Alamofire?

swift
xcode
swift3

#1

Есть вот такой json:

{
    "article": [
        {
            "header": "mindshare payment",
            "items": [
                {
                    "title": "Applications workforce",
                    "text": "Nulla molestiae aliquid natus libero placeat fugit recusandae tempora et."
                }
            ]
        },
        {
            "header": "mindshare payment",
            "items": [
                {
                    "title": "Nulla molestiae",
                    "text": "Applications workforce aliquid natus libero placeat fugit recusandae tempora et."
                }
            ]
        }
    ]
}

Пробовал таким способом, но не помогает:

if let result = response.result.value as? [String: Any],
	let articleArray = result["article"] as? [[String: Any]],
	let main = articleArray["items"] as? [[String: Any]] {
	for array in main {
		guard
		let title = array["title"] as! String,
		let text = array["text"] as! String
		else {return}
		let struct = Articles(title: title, text: text)
	}
}

Эти способы подсказали:

if let result = response.result.value as? [String: Any],
        let articleArray = result["article"] as? [[String: Any]] {
        articleArray.forEach { (obj: [String: Any]) in
            guard let title = obj["title"] as? String,
                 let text = obj["text"] as? String
            else { return }
            let struct = Articles(title: title, text: text)
        }
    }

        if response.result.isSuccess {
            print("Response success")

            let yourJSON : JSON = JSON(response.result.value!)

            if yourJSON != JSON.null {

                print("Success: Json not nil")

                self.title = yourJSON["article"]["title"].string
                self.text = yourJSON["article"]["text"].string

            } else {
                print("Not success: Json nil")
            }

        } else {
            print("Error: \(String(describing: response.result.error))")  
        }

Но не помогло. :pray:


#2

Swift4. Попробуйте так.

 struct Item : Decodable {
        var title: String
        var text: String
    }

    struct Article : Decodable {
    var header: String
    var items: [Item]
}

struct Response : Decodable {
    var article: [Article]
}

Alamofire.request("ваш юрл").responseData { response in
    if let redData = response.result.value {
        let data = try? JSONDecoder().decode(Response.self, from retData)
        print(data)
        //дальше делаете что хотите
        
    }
}

Если вам все же нужен 3 свифт я использовал связку alamofire + objectmapper.. Там найдете примеры.