Mapper issue, JSON response in type AnyObject
Closed this issue · 1 comments
Hello,
I get a response from alamofire of a type JSON like that:
[{
"PDF": [{
"Id": "30",
"Path": "dqzdqdqzdq",
"Nom": "SIO_Ponctuel_E22_14avril2014matin_sujet1_valide.pdf",
"datepublication": "2016-05-30",
"Description": "Niveau 1 PHP",
"id_categorie": "1",
"Id_membre": "1",
"id_reduction": null,
"id_prix": null,
"NbTelechargements": "0"
}, {
"Id": "31",
"Path": "dzqdqzdzq",
"Nom": "2015-2016_Referentiel_RapportActivite_B1_B2.pdf",
"datepublication": "2016-04-25",
"Description": "Niveau 1 HTML5",
"id_categorie": "2",
"Id_membre": "1",
"id_reduction": null,
"id_prix": null,
"NbTelechargements": "0"
}, {
"Id": "32",
"Path": "fdzdqdzqdq",
"Nom": "CV-EPSI.pdf",
"datepublication": "2016-06-04",
"Description": "Niveau 1 WORDPRESS",
"id_categorie": "5",
"Id_membre": "1",
"id_reduction": null,
"id_prix": null,
"NbTelechargements": "0"
}]
}]
and i made the class PDF like that:
import Foundation
import Alamofire
import ObjectMapper
class PDF : Mappable{
var Description: String
var Id: Int
var Nom: String
var Path: String
var datepublication: String
var id_categorie: Int
var id_reduction: Int
func newInstance(map: Map) -> Mappable? {
return PDF(map)
}
required init?(_ map: Map) {
mapping(<#T##map: Map##Map#>)
}
func mapping(map: Map) {
Description <- map[""]
Id <- map[""]
Nom <- map[""]
Path <- map[""]
datepublication <- map[""]
id_categorie <- map[""]
id_reduction <- map[""]
}
}
and the function which have to parse json in object is:
func loadDataPDF(){
Alamofire.request(.GET, "URL", encoding:.JSON).responseJSON{
response in switch response.result{
case.Success(let JSON):
print("Success with JSON : \(JSON)")
do {
var listePDF = Mapper<PDF>.map(JSON)
} catch {
print("error")
}
case.Failure(let error):
print(error)
}
}
}
But the function .map(**) --> does not accept the type "AnyObject", but i can't find a way to get the variable JSON any other type than AnyObject.
If you could answer me that would really great! I'd be so thankful!
You make a great job guys keep going!
The Swift equivalent of that JSON response is:
[[String : [PDF]]]
You have an array of dictionaries (with only one dictionary in it). The one dictionary contains a string that maps to an array of PDFItems with the key "PDF".
To make it work with the existing JSON format:
var listePDF = Mapper<[PDF]>.map(JSON)
print(listePDF[0].pdf![0].Description!)
class PDF: Mappable {
var pdf: [PDFItem]?
required init?(_ map: Map) {
}
func mapping(map: Map) {
pdf <- map["PDF"]
}
}
class PDFItem: Mappable {
var Description: String?
var Id: Int?
var Nom: String?
var Path: String?
var datepublication: String?
var id_categorie: Int?
var id_reduction: Int?
required init?(_ map: Map) {
}
func mapping(map: Map) {
Description <- map["Description"]
Id <- map["Id"]
Nom <- map["Nom"]
Path <- map["Path"]
datepublication <- map["datepublication"]
id_categorie <- map["id_categorie"]
id_reduction <- map["id_reduction"]
}
}