Other options than NSJSONWritingPrettyPrinted?

Convert NSDictionary

to JSON NSData

with the following line:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:answers 
                                                   options:NSJSONWritingPrettyPrinted 
                                                     error:&err];

      

And pass it to the server side, which is a PHP script. The script reads a JSON string as:

{
  "A" : "1941",
  "D" : "1699",
  "B" : "1949",
  "E" : "1823",
  "C" : "1999"
}

      

How can I format the JSON string as 1 string like below?

{"A" : "1941", "D" : "1699", "B" : "1949", "E" : "1823", "C" : "1999"}

      

Is there any parameter other than NSJSONWritingPrettyPrinted

?

+3


source to share


2 answers


Quoth documentation forNSJSONWritingPrettyPrinted

;

If this parameter is not set, the most compact JSON representation is generated.



If you don't want to set any bits in the write parameter mask, just omit zero for this parameter. (Or, in Swift, an empty parameter set that looks like an empty array:. []

)

+9


source


You shouldn't use it NSJSONWritingPrettyPrinted

for purposes other than debugging. You can go to options:0

(see below) to have minified JSON.

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:answers options:0 error:&err];

      

To test this, you can convert it to string and NSLog

it.



NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

      

There are no other options besides NSJSONWritingPrettyPrinted

:

typedef NS_OPTIONS(NSUInteger, NSJSONWritingOptions) {
    NSJSONWritingPrettyPrinted = (1UL << 0)
} NS_ENUM_AVAILABLE(10_7, 5_0);

      

+4


source







All Articles