Question

I run this sample of code

 NSString *st =@"{ \"Hello\" : [ {\"m\":\"m\"} ] }";

NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:[st dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

and i get the output in dic:

{
    Hello =     (
                {
            m = m;
        }
    );
}

replace the square bracket "[" to "(" ,, Why ??

Was it helpful?

Solution

JSON uses square brackets [ ... ] for an array, see http://json.org.

The description method of NSArray uses parentheses ( ... ) to print an array. That format is described in Old-Style ASCII Property Lists.

You cannot expect the output to be equal. As another example, strings in JSON are always enclosed in quotation marks: "m", and the "Old-Style ASCII Property Lists" format omits the quotation marks for purely alphanumeric strings without spaces.

OTHER TIPS

This hasn't actually change anything, it's just a question of formatting. It's customary in Cocoa and ObjC to represent arrays in () instead of the [] used in javascript. For example, try:

NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSLog(@"%@", array);

And you will see:

2014-02-02 10:47:41.468 Untitled[2773:507] (  
    one,  
    two,  
    three  
)

NSArray just uses parens to describe itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top