I am already saturated looking into other solutions but none will work, so here it goes: I want to convert this STRING VALUE , I repeat: STRING VALUE, to a JSONObject or JSONArray:
[["demo": "Default", "tint": "ff00fd", "icon": "http://someurl.com/icon.jpg", "language": "en-US", "endpoint": "http://pres.artifutions.com/traveldemoapp/", "title": "Travel Demo"], ["demo": "Demo 2", "tint": "ff99fd", "icon": "http://someurl.com/icon.jpg", "language": "en-US", "endpoint": "http://pres.artifutions.com/traveldemoapp/", "title": "Second Demo"], ["demo": "Demo 3", "tint": "ff99fd", "icon": "http://someurl.com/icon.jpg", "language": "en-US", "endpoint": "http://pres.artifutions.com/traveldemoapp/", "title": "Third Demo"], ["demo": "Demo 4", "tint": "ff99fd", "icon": "http://someurl.com/icon.jpg", "language": "en-US", "endpoint": "http://pres.artifutions.com/traveldemoapp/", "title": "Fourth Demo"], ["title": "zz", "tint": "ff00fd", "icon": "(default)", "language": "nld-NLD", "endpoint": "http://pres.artifutions.com/traveldemoapp/", "demo": "z"], ["demo": "Add Demo..."]]
When I try to convert that string to a JSON Object, with this code, it crashes:
let jConfigs = JSON(myString).array
print("=======json")
print(jConfigs![0])
What is the problem? Converting that into Dictionary<String,String>
would also work fine for me.
UPDATE: The accepted answer works, I used replace() to clean the input string. Plus, I did an extra step to complete the conversion of the String into a JSON array. Note that in my case, I cannot control how the string comes, it is as it -is-.
var ss=InputString.replace("], [",withString: "}, {")
ss=ss.replace("[[",withString: "[{")
ss=ss.replace("]]",withString: "}]")
if let data = ss.dataUsingEncoding(NSUTF8StringEncoding){
do{
if let array = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? [AnyObject] {
print(array)
}
}
}