-1

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,

kb920
  • 3,039
  • 2
  • 33
  • 44
Len_X
  • 825
  • 11
  • 29
  • 1
    1) Don't paste the output as an image. Copy and paste the actual output (and be sure it's formatted). 2) What format do you want? Put that info in your question as well. – rmaddy Oct 26 '16 at 01:52

2 Answers2

1

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

bdjett
  • 514
  • 5
  • 9
1

The output of NSJSONSerialization.dataWithJSONObject is valid JSON, and that's what you should send to an API. Conversion to a string is pointless unless you want to view the output for debugging purposes.

NSJSONWritingOptions.PrettyPrinted is useful if you want to view the output for debugging purposes, but otherwise it's just a waste of time and makes the JSON a few percent bigger which takes memory, time to send to the API, time to process in the API.

PS. If "Pretty printed" or not makes any difference to your API, then your API is pretty badly broken.

gnasher729
  • 51,477
  • 5
  • 75
  • 98