-2

I'm calling an api in swift 3. And from the api I'm getting response in JSON String format. How to convert that JSON String to Dictionary or Array in Swift. Also that JSON string contains further array's and dictionaries.

I've tried using EVReflection tool. It's only converting the upper JSON object in dictionary. The nested dictionaries and array's are still in String format.

Rohitax Rajguru
  • 893
  • 2
  • 13
  • 35

2 Answers2

0

Try something like this, response is [String: Any] so you must cast to a any type

DispatchQueue.main.async {
    do {
        if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {
            let j = json as NSDictionary
            guard let resp = response as? HTTPURLResponse
                else {
                    // No response from server
                    return
            }

            if resp.statusCode == 200 {
                // You can put here some callback, this is a data you need
            } else {
               // Here is some error
            }
        }
    } catch {
        // And this is a error from server
    }
}
miff
  • 185
  • 4
  • 15
0

You should receive jsonData which you can directly try JSONSerialization.

If for any reason you receive it as json String then you have to convert that string to jsonData first

// Here you have to convert Json String to jsonData
// if it's not Json String then skip this line
let jsonData = jsonString.data(using: .utf8)

// Try to convert it to json object
do {
       let json = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String : Any]
       // now you can try to parse your json


      } catch let error as NSError {
          print(error.localizedDescription)
      }

Or as @Jay mentioned use Swiftyjson

Khalid Afridi
  • 913
  • 5
  • 12
  • `You have to change that string to jsonData` No, they have to get proper JSON data from the server in the first place instead of a JSON string. ;) – Eric Aya May 09 '17 at 08:34
  • My answer is based on the Question convert the json string to properly json Object. There can be many reasons to have your json as string which you have to manually convert it back to json data. – Khalid Afridi May 09 '17 at 08:50
  • I didn't say your answer is wrong (your answer's only "problem" is that this content has already been given many, many times). What I'm saying is that, often, this JSON string will have been made from JSON data anyway... resulting in useless conversions. Common mistake OP should be aware of. – Eric Aya May 09 '17 at 09:20
  • Yes your right. I will update my answer so he understand – Khalid Afridi May 09 '17 at 09:26
  • As always, never use `.mutableContainers` in Swift. The option is completely meaningless (as well as `.mutableLeaves`). Remove the `options` parameter. And the JSON dictionary in Swift 3 is `[String:Any]` – vadian May 09 '17 at 09:40
  • I had left the option there so he can chose from different options in case he want's. Thanks – Khalid Afridi May 09 '17 at 09:47