ios - 如何在 swift 3 中从另一个 JSON 对象获取 JSON 对象

标签 ios json swift

如何在下面的代码中获取 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/

相关文章:

ios - 取消选择表行删除索引行

json - Swift 4 将数据从 json 保存到数组以在 TableView 中显示

ios - 从 Core Data 中的 AppDelegate 执行回滚功能时需要访问相同的 ManagedObjectContext 实例

c# - 如何将 Json 字符串反序列化为自定义 DTO C# 对象?

ios - 手动设置 UITabBar selected Item 时遇到问题

Python TypeError : expected string or buffer. 需要字典中的响应

python - 来自 Django 的 JsonResponse - 显示的值不正确

iphone - AddSubview 和 PresentModal 问题

iphone - QLPreviewController View

ios - 安装和使用 pod