2017-03-07 43 views
3

這裏嵌套數組我返回JSON:映射的Json使用ObjectMapper斯威夫特3

[ { 
    "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 
     } 
    ], 
}, 
.... 
} ] 

這裏是我的模型:

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"] 
    } 
} 

我怎麼能做到這一點至今:

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) 
      } 

輸出:

[ 
    [ 
     "options": ["QuestionID": 110, "OptionText": "..."], 
     ["QuestionID": 110, "OptionText": "..."], 
     ["QuestionID": 110, "OptionText": "..."], 
     ["QuestionID": 110, "OptionText": "..."] 
    ] 
], [ 
    "options": [ 
     [ 
      "QuestionID": 117, "OptionText": "..." 
     ], 
     ["QuestionID": 117, "OptionText": "..."], 
     ["QuestionID": 117, "OptionText": "..."] 
    ] 
], 
...... 
] 

它映射只是嵌套數組,其中是SurveyID,問題等我怎樣才能完成這正確的格式?我搜索了網絡嵌套數組映射,但我找不到任何有用的信息。任何幫助,將不勝感激。

+0

你能發佈你的Map,Mapper和Mappable的實現嗎? – ZeMoon

+0

你是什麼意思,我只是發佈整個可映射類?你需要什麼具體的 –

回答

2

問題是:我沒有很好地分析返回的JSON。 SurveyID,Question等是「問題」對象的元素。所以我編輯我可映射類象下面這樣:

ID <- map["question.ID"] 
    SurveyID <- map["question.SurveyID"] 
    Question <- map["question.Question"] 
    QuestionType <- map["question.QuestionType"] 
    QuestionOrder <- map["question.QuestionOrder"] 

這工作在我的情況。我希望這個解決方案能幫助某人。快樂編碼