Есть вот такой 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))")
}
Но не помогло.