Reputation: 73
Here is my returning JSON :
[ {
"question": {
"ID": 110,
"SurveyID": 12,
"Question": "...",
"QuestionType": "CTS",
"QuestionOrder": 1,
"Status": "1",
"QuestionSubType": null,
"QuestionIndex": null
},
"options": [{
"ID": 311,
"SurveyID": 12,
"QuestionID": 110,
"OptionText": "...",
"IsOther": null,
"Status": "1",
"ImageUrl": null,
"BranchId": null,
"NextQuestionID": null
},
{
"ID": 312,
"SurveyID": 12,
"QuestionID": 110,
"OptionText": "...",
"IsOther": null,
"Status": "1",
"ImageUrl": null,
"BranchId": null,
"NextQuestionID": null
},
{
"ID": 313,
"SurveyID": 12,
"QuestionID": 110,
"OptionText": "...",
"IsOther": null,
"Status": "1",
"ImageUrl": null,
"BranchId": null,
"NextQuestionID": null
},
{
"ID": 314,
"SurveyID": 12,
"QuestionID": 110,
"OptionText": "...",
"IsOther": null,
"Status": "1",
"ImageUrl": null,
"BranchId": null,
"NextQuestionID": null
}
],
}, {
"question": {
"ID": 117,
"SurveyID": 12,
"Question": " ...",
"QuestionType": "CTS",
"QuestionOrder": 2,
"Status": "1",
"QuestionSubType": null,
"QuestionIndex": null
},
"options": [{
"ID": 315,
"SurveyID": 12,
"QuestionID": 117,
"OptionText": "...",
"IsOther": null,
"Status": "1",
"ImageUrl": null,
"BranchId": null,
"NextQuestionID": null
},
{
"ID": 316,
"SurveyID": 12,
"QuestionID": 117,
"OptionText": "...",
"IsOther": null,
"Status": "1",
"ImageUrl": null,
"BranchId": null,
"NextQuestionID": null
},
{
"ID": 317,
"SurveyID": 12,
"QuestionID": 117,
"OptionText": "...",
"IsOther": null,
"Status": "1",
"ImageUrl": null,
"BranchId": null,
"NextQuestionID": null
}
],
},
....
} ]
Here is my models :
class Question : Mappable{
var ID : Int?
var SurveyID : Int?
var Question : String?
var QuestionType : String?
var QuestionOrder : Int?
var options : [Option]?
required init?(map : Map){}
func mapping(map: Map) {
options <- map["options"]
ID <- map["ID"]
SurveyID <- map["SurveyID"]
Question <- map["Question"]
QuestionType <- map["QuestionType"]
QuestionOrder <- map["QuestionOrder"]
}
}
class Option : Mappable{
var QuestionID : Int?
var OptionText : String?
var ImageUrl : String?
var NextQuestionID : Int?
required init?(map: Map) {
}
func mapping(map: Map) {
QuestionID <- map["QuestionID"]
OptionText <- map["QuestionText"]
ImageUrl <- map["ImageUrl"]
NextQuestionID <- map["NextQuestionID"]
}
}
How I could do it so far :
var questions = [Question]()
Alamofire.request(retrieveQuestionsURL(1, branchId: 1, surveyId: 12) , method: .get , headers : headers).responseJSON{
response in
switch response.result{
case .success (let data):
self.questions = Mapper<Question>().mapArray(JSONArray: (data as? [[String: Any]])!)!
case .failure (let err):
print(err)
}
self.removeAllOverlays()
let json = Mapper().toJSONArray(self.questions)
print("questions" , json)
}
Output :
[
[
"options": ["QuestionID": 110, "OptionText": "..."],
["QuestionID": 110, "OptionText": "..."],
["QuestionID": 110, "OptionText": "..."],
["QuestionID": 110, "OptionText": "..."]
]
], [
"options": [
[
"QuestionID": 117, "OptionText": "..."
],
["QuestionID": 117, "OptionText": "..."],
["QuestionID": 117, "OptionText": "..."]
]
],
......
]
It mapped just nested array , where is SurveyID,Question etc. How can i accomplish this correctly formatted? I searched web nested array mapping but I couldn't find any useful information. Any help would be appreciated.
Upvotes: 3
Views: 1814
Reputation: 73
The problem is : I didn't analyze returning JSON very well. SurveyID , Question etc. is an element of "question" object. So I edited my mappable classes like below :
ID <- map["question.ID"]
SurveyID <- map["question.SurveyID"]
Question <- map["question.Question"]
QuestionType <- map["question.QuestionType"]
QuestionOrder <- map["question.QuestionOrder"]
This works in my case. I hope this solution will help somebody. Happy coding
Upvotes: 2