Ios 如何在Swift中解析JSON数组?
我是Swift的初学者。我正在尝试用Swift发出http请求 我成功地做到了这一点,但我无法解析响应。我在这上面找了很多,但什么也没找到 这是我的代码 上述代码的输出为:Ios 如何在Swift中解析JSON数组?,ios,json,swift,Ios,Json,Swift,我是Swift的初学者。我正在尝试用Swift发出http请求 我成功地做到了这一点,但我无法解析响应。我在这上面找了很多,但什么也没找到 这是我的代码 上述代码的输出为: { Login = { AccountType = Pro; FileSizeLimit = 157286400; PasswordHash = fc27a61bc3dda36ebe28887d4da16df2093bbbcd8d58cfc87b2b48b708
{
Login = {
AccountType = Pro;
FileSizeLimit = 157286400;
PasswordHash = fc27a61bc3dda36ebe28887d4da16df2093bbbcd8d58cfc87b2b48b70801c411;
Session = {
id = ca77dd997db2169908a1a84204c99ae7;
};
SmugVault = 0;
User = {
DisplayName = "smug";
NickName = better1;
id = 2455641;
};
};
method = "smugmug.login.withPassword";
stat = ok;
}
我想获取显示名称。我可以提取sessionId,但不能提取名称。请帮我做这个
下面是代码
func postDataToURL(){
问题解决了,解决方案是
let jsonData = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMappedIfSafe)
do {
let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
print("jsonResult : ",jsonResult)
print("Session : ",jsonResult.valueForKey("Login")?.valueForKey("Session"))
print("DisplayName : ",(jsonResult.valueForKey("Login")?.valueForKey("User")?.valueForKey("DisplayName"))!)
} catch {
}可能的重复项显示您的代码如何获取会话ID。要获得DisplayName,它应该非常类似于我添加的代码的可能副本。在JSON中任何地方都看不到数组。
let jsonData = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMappedIfSafe)
do {
let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
print("jsonResult : ",jsonResult)
print("Session : ",jsonResult.valueForKey("Login")?.valueForKey("Session"))
print("DisplayName : ",(jsonResult.valueForKey("Login")?.valueForKey("User")?.valueForKey("DisplayName"))!)
} catch {