[英]JSON result nil after parsing
I am trying to extract the value from a JSON serialization but getting nil as the result. 我正在尝试从JSON序列化中提取值,但结果为nil 。
App was working under Swift2 so its the conversion to Swift 3 where the issue started. App在Swift2下运行,因此将其转换为问题开始的Swift 3。
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
print(jsonResult!)
let mySuccess = jsonResult?["success"] as? Int
print (mySuccess!)
The print(jsonResult!) gives the following output print(jsonResult!)提供以下输出
{
"full_login" = 0;
"logged_in" = 1;
message = "<null>";
success = 1;
}
So all good so far and my parsing is working and I now have the data from the server. 到目前为止一切顺利,我的解析工作正常,现在我从服务器获取了数据。
However print(mySuccess!) gives this output 但是print(mySuccess!)提供此输出
fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:解开Optional值时意外发现nil
So I understand the output saying that the code found nil while unwrapping, so my issue now is how do I extract the value of the "Success" key as it was behaving under Swift 2 but now not so under Swift 3? 所以我理解输出说代码在解包时发现nil,所以我的问题现在是如何提取“成功”键的值,因为它在Swift 2下表现良好,但现在在Swift 3下却表现不佳?
UPDATE 更新
Sneak found a possible issue that success = 1 do not have the "" so will update question answer once I investigate. Sneak发现了一个可能的问题,即成功= 1没有“”,因此一旦我进行调查,就会更新问题的答案。
hi you can use the concept of OPTIONAL BINDING to check for nil values. 嗨,您可以使用可选绑定的概念来检查nil值。
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
print(jsonResult!)
if let mySuccess = jsonResult?["success"] as? Bool
{
print (mySuccess)
}
else
{
print ("Found nil")
}
Your print log of success = 1;
您的打印success = 1;
日志success = 1;
demonstrates that jsonResult?["success"]
is not nil
. 演示jsonResult?["success"]
不是nil
。 JSONSerialization.jsonObject can only return three things that would display as 1
: a String, a Number or value true
. JSONSerialization.jsonObject只能返回显示为1
三件事:字符串,数字或值true
。 As you tried to unwrap it as an Int
and it failed, the only left possibilities are that it was a String
or a Bool
. 当您尝试将其作为Int
展开时,它失败了,唯一剩下的可能是String
或Bool
。
You probably have a success
as either: 您可能会success
因为:
"success": true
Or: 要么:
"success": "1"
As such, you may want to do: 因此,您可能需要执行以下操作:
let mySuccess = jsonResult?["success"] as? Bool
Or: 要么:
let mySuccess = jsonResult?["success"] as? String
Or ask a change in the backend API response. 或要求更改后端API响应。
Ok so the comments were really helpful and helped me hone into the problem. 好的,这样的评论真的很有帮助,并帮助我完善了这个问题。 I started to look at the missing "" but noticed that when I entered the following code: 我开始查看缺少的“”,但是注意到当我输入以下代码时:
for (key, value) in jsonResult!
{
print (key)
print (value)
}
I get the following output 我得到以下输出
logged_in
1
full_login
0
success
1
message
<null>
So there had to be a way to get just the value for success . 因此,必须有一种方法来获取成功的价值。
This page gave me the solution in the end. 本页最后给出了解决方案。 I had to use the following code: 我必须使用以下代码:
let myResult = (jsonResult?["success"])
print("SUCCESS VALUE >> ", myResult!)
This now gives me the value of 1 and all solved. 现在,这给了我1的值,并且全部求解。 Thanks again for the comments as they helped. 再次感谢您提供的帮助。
I then had the issue of not realising the success value was a Bool so I had to use the following code to check for true or false: 然后,我遇到了一个问题,那就是没有意识到成功值是一个布尔值,因此我不得不使用以下代码来检查是非值:
if (jsonResult?["success"] as? Bool)!
{
okToLogIn = true
print(okToLogIn)
}
else
{
okToLogIn = false
print(okToLogIn)
}
Now all good and app working again under Swift 3. 现在一切正常,应用程序又可以在Swift 3下正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.