为什么我的JSON对象的NSDictionary引用了某些值而不是其他值? [重复]

时间:2022-06-28 07:30:34

This question already has an answer here:

这个问题在这里已有答案:

- (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");

yeilds this in Output terminal:

在输出终端中显示这个:

 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

I don't understand why, if all the json values are enclosed in quotations, only 2/4 dictionary items include them. What is NSDictionary supposed to store by default?

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

2 个解决方案

#1


2  

Only those values are quoted in the description which aren't proper identifiers (i. e. there are spaces, special characters, and not just alphanumeric characters). The description of the dictionary doesn't print the keys and values as-is. (Specifically, they aren't actually quoted).

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

This has nothing to do with them being quoted in the JSON. In JSON, every string is quoted, always.

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

#2


1  

Strings with spaces and special characters like dot are surrounded by " to denote that it is single entity. Normal words and numbers are self explanatory.

带有空格和像dot这样的特殊字符的字符串被“表示它是单个实体”所包围。正常的单词和数字是自我解释的。

#1


2  

Only those values are quoted in the description which aren't proper identifiers (i. e. there are spaces, special characters, and not just alphanumeric characters). The description of the dictionary doesn't print the keys and values as-is. (Specifically, they aren't actually quoted).

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

This has nothing to do with them being quoted in the JSON. In JSON, every string is quoted, always.

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

#2


1  

Strings with spaces and special characters like dot are surrounded by " to denote that it is single entity. Normal words and numbers are self explanatory.

带有空格和像dot这样的特殊字符的字符串被“表示它是单个实体”所包围。正常的单词和数字是自我解释的。