简体   繁体   中英

JSON result nil after parsing

I am trying to extract the value from a JSON serialization but getting nil as the result.

App was working under Swift2 so its the conversion to Swift 3 where the issue started.

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

{
"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

fatal error: unexpectedly found nil while unwrapping an Optional value

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?

UPDATE

Sneak found a possible issue that success = 1 do not have the "" so will update question answer once I investigate.

hi you can use the concept of OPTIONAL BINDING to check for nil values.

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; demonstrates that jsonResult?["success"] is not nil . JSONSerialization.jsonObject can only return three things that would display as 1 : a String, a Number or value 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 .

You probably have a success as either:

"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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM