[英]How to obtain the JSON data as nil when saving in swift3?
Code: 码:
var json: AnyObject?
do {
json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any] as AnyObject?
let UserID = json?["UserID"] as? [String:AnyObject]
print(UserID) //printing nil
}
catch {
print(error)
}
Assuming the root object of the JSON is a dictionary and the value for userID
a string it might be 假设JSON的根对象是一个字典,而
userID
的值是一个字符串,则它可能是
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:Any] {
if let userID = json["UserID"] as? String { // or as? Int if the user id is an integer
print(userID)
}
}
}
catch {
print(error)
}
.allowFragments
is nonsense if the root object is supposed to be a collection type. 如果认为根对象是集合类型,则
.allowFragments
是无意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.