You have invalid JSON,
According to your json the
authors are a dictionary , but the authorβs dictionary is missing
As well as inside the array there is a dictionary, but there are no brackets and a comma
Your correct json
authors: [
{
name: "",
id: 0
}
]
And then your code looks fine to me
if json.count > 0 {
for item in json["authors"]{
print(item)
authorArray.append(Mapper<Author>().map(JSONObject: item)!)
}
}
EDIT
try it
if json.count > 0 {
for item in json["authors"]{
print(item)
do {
let objectData = try JSONSerialization.data(withJSONObject: item, options: .prettyPrinted)
let string = String(data: objectData, encoding: .utf8)
authorArray.append(Mapper<Author>().map(JSONObject: string)!)
} catch {
print(error)
}
}
}
source
share