Lenx van Zyl Lenx van Zyl - 1 month ago 5
JSON Question

Convert NSString to JSON

I am converting a

[NSDictionary]
to
NSString
like so :

do{

let newDict = try NSJSONSerialization.dataWithJSONObject(objectDictionaries, options: NSJSONWritingOptions.PrettyPrinted)
if let json = NSString(data: newDict, encoding: NSUTF8StringEncoding) {
print(json)
}


When the
json
prints out I get this:

enter image description here

How do I convert this NSString to JSON without changing the format as this photo shows?
The I want to send this json to a API and the format I want is the only one the API will be able to encode,

Answer

Remove the option NSJSONWritingOptions.PrettyPrinted in the call to dataWithJSONObject. That is what is forcing the outputted string to be formatted like that.

Comments