Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : JSONSerialization.jsonObject not working for json into json

I am using this code for geting json data from url:

URLSession.shared.dataTask(with:request, completionHandler: { (data, response, error) in
 guard let data = data, error == nil else { return }

 do {           
   let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:String]
   completion(json)              
 } catch _ as NSError {
   completion(nil)
 }
}).resume()

If my json is {"error":"true","message":"no"} it works fine

But if i use json into json, it not works :

{"error":"true","message":"no","state":{"id":"1","name":"empty"}}
like image 330
Te Me Avatar asked Aug 17 '26 04:08

Te Me


1 Answers

Using as! [String:String] you are saying that all values in the dictionary will be of String type, but the nested JSON related to the state key is obviously not a String

use as? [String: Any] and then cast your other properties as needed. result["error"] as String

Using this method it makes getting the nested data more difficult than it needs to be:

if let state = result["state"] as? [String: String] {
    let name = state["name"]
    let value = state["value"]
}

Notes

  1. When using Swift 4 or greater you should be using the Codable Protocol
  2. You should not be force unwrapping, use conditional unwrapping (as? [String: Any])

EDIT:

An example of how you can do this using Codable and how it is used.

Playground code

// you dont need this part, I am not making network request
let jsonData = """
{"error":"true","message":"no","state":{"id":"1","name":"empty"}}
""".data(using: .utf8)

struct ErrorState: Codable
{
    let id: String
    let name: String
}

struct ErrorResponse: Codable
{
    let error: String
    let message: String
    let state: ErrorState
}

guard let data = jsonData else { fatalError() }

let errorResponse = try? JSONDecoder().decode(ErrorResponse.self, from: data)
print(errorResponse?.state.name)
like image 58
Scriptable Avatar answered Aug 19 '26 14:08

Scriptable



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!