为什么我的JSON对象的NSDictionary会引用某些值而不是其他值?

时间:2013-05-22 16:01:03

标签: ios objective-c json nsdictionary

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Succeeded! Received %d bytes of data",[_configData length]);

    NSString *responseJSONString = [[NSString alloc] initWithData:_configData encoding: NSASCIIStringEncoding];
    NSLog(@"Response: %@", responseJSONString);

    // convert to dictionary 'settingsDictionary'
    NSError* error = [[NSError alloc] init];
NSDictionary *settingsDictionary = [NSJSONSerialization JSONObjectWithData:_configData options:kNilOptions error:&error];
NSLog(@"Dictionary of JSON objects ::: \n%@", settingsDictionary);   // why?!

    NSLog(@"DONE");

在输出终端中显示这个:

 2013-05-22 11:38:59.318 Tests[8817:c07] didReceiveResponse:
 responseData length:(0) 2013-05-22 11:38:59.320 Tests[8817:c07]
 Succeeded! Received 114 bytes of data 2013-05-22 11:38:59.321
 Tests[8817:c07] Response: {"CustomerName":"Example Company","HostName":"streaming1.mycompany.com\/live","AppName":"streamer","Port":"1935"}
 2013-05-22 11:38:59.321 Tests[8817:c07] Dictionary of JSON objects :::
 {
     AppName = streamer;
     CustomerName = "Example Company";
     HostName = "streaming1.mycompany.com/live";
     Port = 1935; 
 } 
 2013-05-22 11:38:59.322 Tests[8817:c07] DONE

我不明白为什么,如果所有json值都包含在引号中,则只有2/4个字典项包含它们。什么是NSDictionary默认存储?

2 个答案:

答案 0 :(得分:2)

在描述中仅引用那些不是正确标识符的值(即,有空格,特殊字符,而不仅仅是字母数字字符)。字典的描述不按原样打印键和值。 (具体来说,它们并非实际上引用)。

这与它们在JSON中被引用无关。在JSON中,始终引用每个字符串。

答案 1 :(得分:1)

包含空格和dot等特殊字符的字符串被"包围,表示它是单个实体。正常的单词和数字是不言自明的。