如何在下面的代码中获取 applicationStateJson,我可以从数据中解析下面的 JSON 对象,但无法从中获取 applicationStateJson
{
applicationStateJson = {
"address_status" = 0;
email = "xxxxxxxx;
"email_status" = 0;
"first_name" = xxx;
"last_name" = "";
"login_status" = 1;
loginstype = "<null>";
mobile = xxxxxx;
"mobile_status" = 1;
notifications = (
);
"notifications_size" = 0;
"otp_status" = 3;
"profile_id" = "<null>";
"profile_name" = "virtual_recruiter";
"user_id" = 454;
"user_status" = 1;
"virtual_recruiter_id" =xxx;
};
"error_msg" = "<null>";
status = "<null>";
}
编码为
do {
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(json)
let applicationStateJson = try json["applicationStateJson"]
} catch { print(error.localizedDescription) }
最佳答案
试试下面的代码
首先你需要改变下面一行,它是Dictionary<String, Any>
不是AnyObject
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String, Any>
然后通过下面的代码获取applicationStateJson
if let appJson = json["applicationStateJson"] as? Dictionary<String, Any> {
print(appJson)
print("Email : \(appJson["email"] as? String ?? "Email not found")") // For get email
}
关于ios - 如何在 swift 3 中从另一个 JSON 对象获取 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47261509/