Response String :
{"status":"failure","message":"link_inactive"}
I would like the same format with NsmutableDictionary
or NSDictionary
so I can access via key value.
Response String :
{"status":"failure","message":"link_inactive"}
I would like the same format with NsmutableDictionary
or NSDictionary
so I can access via key value.
you can do this by JSONObjectWithData method of NSJSONSerialization.
do {
let jsonDict: NSDictionary? = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as? NSDictionary
if let jsonDict = jsonDict {
print(jsonDict["message"])
} catch let error as NSError {
// error handling
debugLog(error)
}
Use this code it will help you
func convertStringToDictionary(text: String) -> [String:AnyObject]? {
if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
do
{
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? [String:AnyObject]
return json
}
catch
{
print("error")
}
}
return nil
}
as Eric D said the original code:
https://stackoverflow.com/a/33173192/846780
Try it:
func convertStringToDictionary(text: String) -> [String:AnyObject]? {
if let data = text.dataUsingEncoding(NSUTF8StringEncoding) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? [String:AnyObject]
return json
} catch {
print("Something went wrong")
}
}
return nil
}
let string = "{\"status\":\"failure\",\"message\":\"link_inactive\"}"
let dict = convertStringToDictionary(string) //["status": "failure", "message": "link_inactive"]