1

I have my DataModel as this and it has a function name toJSON which converts this object to string representation of JSON

ContactEntry: NSObject {
    var name: String!
    var phone: String?
    var email: String?

    func toJson() -> String {
    var dict : [String: AnyObject] = [:]
    dict["name"] = (self.name ??  "") as AnyObject
    dict["phone"] = (self.phone ?? "") as AnyObject
    dict["email"] = (self.email ?? "") as AnyObject


    guard let data = try? JSONSerialization.data(withJSONObject: dict, options: []) else{
        return "{}"
    }

    guard let jsonString = String(data: data, encoding: String.Encoding.utf8) else {
        return "{}"
    }
    return jsonString
}
}

It works very fine until now as I am getting correct JSON representation for this object.

Now I have to convert an array of ContactEntry to JsonArray.

I am doing this way but I am getting very weird symbols in the result string. Let's say

var contacts = [ContactEntry]()
var contactsStr = [String]()
...
for contact in contacts{
    contactsStr.append(contact.toJson())
}

do{
    let data = try? JSONSerialization.data(withJSONObject: contactsStr, options: [])

    let jsonString = try? String(data: data!, encoding: String.Encoding.utf8)

    print(jsonString)
}

I am getting the output like this

[\n \"{\\\"phone\\\":\\\"+XXXX\\\",\\\"name\\\":\\\"YYYY\\\",\\\"email\\\":\\\"\\\"},\",\n \".........\n]

Please help me to do it in a better and clean way

Thanks for your help

7
  • Can you share the print of contactsStr? Commented Nov 13, 2016 at 21:11
  • Forget prettyPrinted, that causes most of the weird output Commented Nov 13, 2016 at 21:14
  • @Frankie This is how contactStr looks ["{\"phone\":\"+xxx\",\"name\":\"yyy\",\"email\":\"\"}", "{\"phone\":\"+xxx\",\"name\":\"yy\",\"email\":\"\"}", "{\"phone\":\"xxx\",\"name\":\"yyy\",\"email\":\"\"}", "{\"phone\":\"+yyy\",\"name\":\"xxxx\",\"email\":\"\"}", "{\"phone\"... Commented Nov 13, 2016 at 21:36
  • @vadian Is this the best way to convert to JSON? I don't have much of an experience in Swift but I am sure JSON serialisation and deserialisation is very common in Swift. Just wanted to know right practice Commented Nov 13, 2016 at 21:37
  • @vadian I am not sure if I am following right technique. Commented Nov 13, 2016 at 21:38

1 Answer 1

1

I think you are double serializing your output. That's why I asked for the contact string.

Try:

func toJson() -> [String: AnyObject] {
    var dict : [String: AnyObject] = [:]
    dict["name"] = (self.name ??  "") as AnyObject
    dict["phone"] = (self.phone ?? "") as AnyObject
    dict["email"] = (self.email ?? "") as AnyObject
    return dict
}

With:

var contactsStr = [[String: AnyObject]]()

And print out the serialization. It should just be singly escaping quotes such as \"name\". Not \\"name\\"

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

Comments

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.