如何使用ObjectMapper映射此JSON?

时间:2016-06-15 11:54:55

标签: ios json swift objectmapper

我从Web服务获得此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"}]
 }

这就是我到目前为止所拥有的

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

}
} 

我不知道如何映射这个JSON响应所以请你指导我如何映射这个JSON ???

1 个答案:

答案 0 :(得分: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"]
        }
    }