Convert Json string to Json object in Swift 4

后端 未结 4 1201
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 06:13

I try to convert JSON string to a JSON object but after JSONSerialization the output is nil in JSON.

Response String:

[{\\\         


        
4条回答
  •  孤城傲影
    2020-12-08 06:42

    I tried the solutions here, and as? [String:AnyObject] worked for me:

    do{
        if let json = stringToParse.data(using: String.Encoding.utf8){
            if let jsonData = try JSONSerialization.jsonObject(with: json, options: .allowFragments) as? [String:AnyObject]{
                let id = jsonData["id"] as! String
                ...
            }
        }
    }catch {
        print(error.localizedDescription)
    
    }
    

提交回复
热议问题