Swift 3使用Object Mapper将字符串解析为Array返回nil

时间:2016-11-19 16:03:41

标签: ios swift swift3

我有简单的JSON:

[{"date":1478962387,"operationId":-1,"id":330,"full_name":"Listonosz"},{"date":1478962039,"operationId":1,"id":329,"full_name":"Listonosz"},{"date":1478961989,"operationId":1,"id":328,"full_name":"Listonosz"},{"date":1478961684,"operationId":1,"id":327,"full_name":"Listonosz"},{"date":1478961650,"operationId":1,"id":326,"full_name":"Listonosz"},{"date":1478961050,"operationId":1,"id":325,"full_name":"Listonosz"},{"date":1478960996,"operationId":1,"id":324,"full_name":"Listonosz"},{"date":1476180920,"operationId":1,"id":323,"full_name":"Jan Kowalski"},{"date":1475499172,"operationId":1,"id":322,"full_name":"Jan Kowalski"},{"date":1475499146,"operationId":-1,"id":321,"full_name":"Listonosz"},{"date":1474893426,"operationId":1,"id":320,"full_name":"Jan Kowalski"},{"date":1474466105,"operationId":1,"id":319,"full_name":"Jan Kowalski"},{"date":1474440483,"operationId":1,"id":318,"full_name":"Jan Kowalski"},{"date":1474365451,"operationId":-1,"id":317,"full_name":"Listonosz"},{"date":1474365436,"operationId":1,"id":316,"full_name":"Jan Kowalski"},{"date":1474362566,"operationId":-1,"id":315,"full_name":"Listonosz"},{"date":1474352876,"operationId":1,"id":314,"full_name":"Jan Kowalski"},{"date":1474268567,"operationId":-1,"id":312,"full_name":"Listonosz"},{"date":1474267044,"operationId":1,"id":311,"full_name":"Jan Kowalski"},{"date":1474009069,"operationId":1,"id":310,"full_name":"Jan Kowalski"},{"date":1473923176,"operationId":1,"id":309,"full_name":"Jan Kowalski"},{"date":1473923163,"operationId":-1,"id":308,"full_name":"Listonosz"},{"date":1473923140,"operationId":1,"id":307,"full_name":"Jan Kowalski"},{"date":1473869209,"operationId":1,"id":306,"full_name":"Jan Kowalski"},{"date":1473869190,"operationId":-1,"id":305,"full_name":"Listonosz"},{"date":1473869172,"operationId":1,"id":304,"full_name":"Jan Kowalski"},{"date":1473869043,"operationId":1,"id":303,"full_name":"Jan Kowalski"},{"date":1473868973,"operationId":1,"id":302,"full_name":"Jan Kowalski"},{"date":1473868599,"operationId":-1,"id":301,"full_name":"Listonosz"},{"date":1473866635,"operationId":-1,"id":299,"full_name":"Listonosz"},{"date":1473859656,"operationId":1,"id":298,"full_name":"Jan Kowalski"},{"date":1473832309,"operationId":-1,"id":296,"full_name":"Listonosz"},{"date":1473832289,"operationId":1,"id":295,"full_name":"Jan Kowalski"},{"date":1473748785,"operationId":-1,"id":294,"full_name":"Listonosz"},{"date":1473748768,"operationId":1,"id":293,"full_name":"Jan Kowalski"},{"date":1473748658,"operationId":-1,"id":291,"full_name":"Listonosz"},{"date":1473748615,"operationId":1,"id":290,"full_name":"Jan Kowalski"},{"date":1473746685,"operationId":1,"id":289,"full_name":"Jan Kowalski"},{"date":1473746420,"operationId":1,"id":288,"full_name":"Jan Kowalski"},{"date":1473746401,"operationId":1,"id":287,"full_name":"Jan Kowalski"},{"date":1473691633,"operationId":-1,"id":286,"full_name":"Listonosz"},{"date":1473691617,"operationId":1,"id":285,"full_name":"Jan Kowalski"},{"date":1473691453,"operationId":1,"id":284,"full_name":"Jan Kowalski"},{"date":1473689462,"operationId":1,"id":283,"full_name":"Jan Kowalski"},{"date":1473688455,"operationId":1,"id":282,"full_name":"Jan Kowalski"},{"date":1473687909,"operationId":-1,"id":281,"full_name":"Listonosz"},{"date":1473687803,"operationId":1,"id":279,"full_name":"Jan Kowalski"},{"date":1473687473,"operationId":1,"id":278,"full_name":"Jan Kowalski"},{"date":1473687457,"operationId":-1,"id":277,"full_name":"Listonosz"},{"date":1473345312,"operationId":1,"id":274,"full_name":"Jan Kowalski"},{"date":1473345292,"operationId":-1,"id":273,"full_name":"Listonosz"},{"date":1473345275,"operationId":1,"id":272,"full_name":"Jan Kowalski"},{"date":1473344759,"operationId":1,"id":271,"full_name":"Jan Kowalski"},{"date":1473344746,"operationId":-1,"id":270,"full_name":"Listonosz"},{"date":1473344726,"operationId":1,"id":269,"full_name":"Jan Kowalski"},{"date":1473344703,"operationId":1,"id":268,"full_name":"Jan Kowalski"},{"date":1473344681,"operationId":1,"id":267,"full_name":"Jan Kowalski"},{"date":1473344403,"operationId":1,"id":266,"full_name":"Jan Kowalski"},{"date":1473344379,"operationId":1,"id":265,"full_name":"Jan Kowalski"},{"date":1473344365,"operationId":-1,"id":264,"full_name":"Listonosz"},{"date":1473344352,"operationId":1,"id":263,"full_name":"Jan Kowalski"},{"date":1473344276,"operationId":-1,"id":262,"full_name":"Listonosz"},{"date":1473344257,"operationId":1,"id":261,"full_name":"Jan Kowalski"},{"date":1473344155,"operationId":1,"id":258,"full_name":"Jan Kowalski"},{"date":1473095710,"operationId":-1,"id":251,"full_name":"Listonosz"},{"date":1473095692,"operationId":-1,"id":250,"full_name":"Listonosz"},{"date":1472398084,"operationId":1,"id":246,"full_name":"Jan Kowalski"},{"date":1472398026,"operationId":-1,"id":245,"full_name":"Listonosz"},{"date":1472396278,"operationId":-1,"id":244,"full_name":"Listonosz"},{"date":1472394355,"operationId":-1,"id":243,"full_name":"Listonosz"},{"date":1472394318,"operationId":1,"id":242,"full_name":"Jan Kowalski"}]

这是一个简单的字符串。然后我尝试使用以下方法将其解析为对象数组:

if let arr : Array<PostHistory> = Mapper<PostHistory>().mapArray(JSONObject: json) {
    return arr
}

let arr始终为nil。 我做错了什么?

我的PostHistory课程:

import Foundation
import ObjectMapper

public class PostHistory : Mappable{

    var id :Int!
    var full_name : String!
    var date : Int!
    var operationId :Int!


    public required init?(map: Map) {

    }

    init(id :Int!, full_name :String!, date :Int!,operationId : Int!) {
        self.id = id;
        self.full_name = full_name;
        self.date = date;
        self.operationId = operationId;
    }



    public func mapping(map: Map) {
        id  <- map["id"]
        full_name  <- map["full_name"]
        date  <- map["date"]
        operationId  <- map["operationId"]

    }
}

1 个答案:

答案 0 :(得分:2)

正如您所说,您正在尝试使用JSON字符串而不是JSON对象 请在课程顶部导入

import ObjectMapper

现在,在api reponse中编写上面的代码。 json 是我们在api响应中收到的JsonString

if let arr: Array<PostHistory> = Mapper<PostHistory>().mapArray(JSONString: json) {
    return arr            
}

即。通过JSONString代替JSONObject