如何在下面的代码中获取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) }
答案 0 :(得分:1)
尝试使用以下代码
首先,您需要更改以下行,@foreach (var contact in contactList)
{
<li>@contact</li>
}
而不是Dictionary<String, Any>
AnyObject
然后通过以下代码获取 applicationStateJson
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String, Any>
答案 1 :(得分:0)
JSON的根对象显然是一个字典,而不是AnyObject
(一个对象,但我不知道 - 它是什么)。
键applicationStateJson
的值也是字典。
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String:Any],
let applicationStateJson = json["applicationStateJson"] as? [String:Any] {
print(applicationStateJson)
}
} catch {
print(error)
}
与往常一样,.mutableContainers
在Swift中完全没有意义