0

我的应用程序由 Swift 3.1 编写。在我从服务器获取 json 并将其解析为String之后,我得到了一个奇怪的附加字符。来自服务器的字符串响应是:"name" : "nhatlee's store"但是在我通过代码解析 Json 之后,name = dict["name"] as? String我得到的结果是name = Lee Tester\'s Store. 请帮助我纠正这种情况下解析 json 的方式。这是从服务器获取数据的代码:

self.requestWith(.post, url: url, parameters: params) { (data, error) in
        if let data = data?["mentions"] as? Array<AnyObject>{
            var mentionObjs = [MentionModel]()
            for obj in data{
                print(obj)
                let mention = MentionModel(from: obj as! [String : AnyObject])
                mentionObjs.append(mention)
            }
            Completion(Result.Success(mentionObjs))
        } else {
        Completion(Result.Failure(error))
        }
    }

json响应是:

    {
    email = "nhatlee3@gmail.com";
    id = 516;
    image = "<null>";
    name = nhatlee3;
    "object_class" = User;
}
{
    id = 106;
    image = "<null>";
    name = "Lee Tester's Store";
    "object_class" = Store;
    "user_id" = 352;
}

这是我的模型对象(我将json解析为模型->不确定代码结构的好方法,如果您有更好的方法,请给我一些建议):

    struct MentionModel {
    var id: Int?
    var name: String?
    var imageUrl: String?
    var objectClass: String?
    init(from dict: [String: AnyObject]) {
        id          = dict["id"] as? Int
        name        = dict["name"] as? String
        imageUrl    = dict["image"] as? String
        objectClass = dict["object_class"] as? String
    }
}
4

0 回答 0