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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.