Convert the string to JSON iOS swift

Solution:

Here you can convert the string format of JSON to real JSON.


    func StringToJson(str:String) -> NSMutableArray {
        var data = str.data(using: .utf8)!
        
        let arr = NSMutableArray()
        do {
            
            if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String,Any>]
            {
                arr.add(jsonArray)
            } else {
                print("bad json")
            }
            
        } catch let error as NSError {
            print(error)
            
        }
        
        do {
            
            if let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [String:Any]
            {
                arr.add(jsonArray)
            } else {
                print("bad json")
            }
            
        } catch let error as NSError {
            print(error)
            
        }
        return arr
        
    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift