0
- (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 默认应该存储什么?

4

2 回答 2

2

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

这与它们在 JSON 中被引用无关。在 JSON 中,每个字符串都被引用,总是如此。

于 2013-05-22T16:04:34.933 回答
1

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

于 2013-05-22T16:03:25.937 回答