-1

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)
                       }
                    }
                 }
Josh
  • 6,251
  • 2
  • 46
  • 73
  • 2
    What string? I see an array of dictionaries, not a JSON string. You want `Dictionary` but it is already `[Dictionary]`, you don't need SwiftyJSON, just access the array. There's no JSON in this question. – Eric Aya Mar 14 '16 at 17:03
  • @EricD. It IS a string, the string representation of a [Dictionary] in any case. The Question is: How can I convert it to a JSON object or to a Dictionary object? – Josh Mar 14 '16 at 17:06
  • @EricD. It IS there, if you don't skim through the reading I have underlined it. – Josh Mar 14 '16 at 17:09

1 Answers1

1

You provided invalid json format.

Correct one (curly braces for object):

[{"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"}]

dichen
  • 1,643
  • 14
  • 19
  • You where right, the fault is not so visible, and I cannot control how the string arrives like that. Furthermore, for those interested, I have added an extra step, to convert the whole thing into a JSON shaped array. – Josh Mar 15 '16 at 11:57