2016-06-15 3 views
1

웹 서비스에서이 JSON이 응답 중입니다. 나는 파일의 구조를 이해할 수있다.ObjectMapper를 사용하여이 JSON을 매핑하는 방법은 무엇입니까?

{"response":200,"message":"fetch successfully","data":[ 
{"id":1,"question":"Are you currently exercising regularly?","choices":{"too_lazy":"Too lazy","times_1_3_week":"1-3 times a week","i_love_it":"I just love it!"},"answer_select":"single"}, 
{"id":2,"question":"Are you active member of Gym?","choices":{"yes":"Yes","no":"No"},"answer_select":"single"}] 
} 

이 내가 무엇을 가지고 지금까지 내가이 JSON을 매핑 할 수있는 방법이 JSON 응답 그래서 당신은 저를 인도하시기 바랍니다 수를 매핑하는 방법을 잘 모르겠습니다

import Foundation 
import UIKit 
import ObjectMapper 

class YASUserQuestion: Mappable { 

var question: String 
var id: Int 
var choices: [YASExercisingQuestionChoices] 
required init?(_ map: Map) { 
    question = "" 
    id = 0 
    choices = [] 

} 

func mapping(map: Map) { 

    question  <- map["question"] 
    id    <- map["id"] 
    choices   <- map["choices"] 
} 
} 

class YASExercisingQuestionChoices: Mappable { 

var tooLazy: String 
var times_1_3_Week: String 
var ILoveIt: String 

required init?(_ map: Map) { 

    tooLazy = "" 
    times_1_3_Week = "" 
    ILoveIt = "" 

} 

func mapping(map: Map) { 

    tooLazy    <- map["too_lazy"] 
    times_1_3_Week  <- map["times_1_3_week"] 
    ILoveIt    <- map["i_love_it"] 
} 
} 

class YASGymMemberQuestionChoices: Mappable { 

var yes: String 
var no: String 


required init?(_ map: Map) { 

    yes = "" 
    no = "" 

} 

func mapping(map: Map) { 

    yes    <- map["yes"] 
    no    <- map["no"] 

} 
} 

???

+0

당신은 어떻게 내 말은 당신이 그것을 가져 오는와의 String 또는 사전로 저장되어있다, 서버에서 JSON을 받고 [문자열 : AnyObject] 또는 SwiftyJSON 같은 도우미 라이브러리를 사용하고 있습니까? – iamyogish

+0

한 가지 문제가 있습니다. JSON에서 "choices"는 사전이지만 코드에서 배열을 선언했습니다. – Moritz

답변

1

이것은 수업 구조가됩니다. 이 클래스를 만든 후에는 ObjectMapper를 사용하여 JSON을 매핑하면됩니다.

import UIKit 
import ObjectMapper 
class UserResponse: NSObject,Mappable { 

     var message: String? 
     var response: String? 
     var data: [DataClass]? 

     override init() { 
      super.init() 
     } 

     convenience required init?(map: Map) { 
      self.init() 
     } 


     func mapping(map: Map) { 
      message <- map["message"] 
      response <- map["response"] 
      data <- map["data"] 
     } 
    } 

    class DataClass: NSObject,Mappable { 

     var answer_select: String? 
     var ID: String? 
     var question: String? 
     var choices: [ChoicesClass]? 

     override init() { 
      super.init() 
     } 

     convenience required init?(map: Map) { 
      self.init() 
     } 


     func mapping(map: Map) { 
      answer_select <- map["answer_select"] 
      ID <- map["id"] 
      question <- map["question"] 
      choices <- map["choices"] 
     } 
    } 

    class ChoicesClass: NSObject,Mappable { 

     var i_love_it: String? 
     var times_1_3_week: String? 
     var too_lazy: String? 

     override init() { 
      super.init() 
     } 

     convenience required init?(map: Map) { 
      self.init() 
     } 


     func mapping(map: Map) { 
      i_love_it <- map["i_love_it"] 
      times_1_3_week <- map["times_1_3_week"] 
      too_lazy <- map["too_lazy"] 
     } 
    }