我的应用程序由Swift 3.1编写。在我从服务器获取json并将其解析为String
之后,我得到了一个奇怪的追加字符。服务器的字符串响应是:"name" : "nhatlee's store"
但是在我用代码name = dict["name"] as? String
解析Json之后,我得到的结果是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解析为模型 - &gt;不确定代码结构的好方法,如果你有更好的方法,请给我一些建议):< / p>
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
}
}