Convert the string to JSON iOS swift
Solution:
Here you can convert the string format of JSON to real JSON.
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
Post a Comment