使用ObjectMapper Swift 3将Json映射到嵌套数组

时间:2017-03-07 13:19:01

标签: arrays json swift swift3

这是我返回的JSON:

if(true)
    Test2 obj;

这是我的模特:

if(true) {
    Test2 obj;
}

到目前为止我怎么做:

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

它只映射了嵌套数组,其中是SurveyID,Question等。如何正确格式化?我搜索了Web嵌套数组映射,但我找不到任何有用的信息。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

问题是:我没有很好地分析返回的JSON。 SurveyID,Question等是&#34;问题&#34;的一个元素。宾语。所以我编辑了我的mappable类,如下所示:

li

这适用于我的情况。我希望这个解决方案可以帮助别人。快乐的编码