SBJSON 丢失引号
我正在使用 SBJSON 解析器来解析 JSON 响应:
{"status":0,"sessions":[{"name":"kldlksdklsdkl","active":false,"status":"saved","type":"web","key":"30228ee71f09b93aaa2d1738","contributor_id":"lance","created_at":"Mon May 02, 2011 02:35 PM","closed_at":"Mon May 02, 2011 02:46 PM"}{"name":"Blahieririe","active":false,"status":"saved","type":"web","key":"dbd2bbcc8681bba6a6532051","contributor_id":"lance","created_at":"Mon May 02, 2011 01:42 PM","closed_at":"Mon May 02, 2011 02:34 PM"},{"name":"Jim","active":false,"status":"saved","type":"web","key":"ec5bcf18356a29bb4490841f","contributor_id":"lance","created_at":"Fri April 29, 2011 02:37 PM","closed_at":"Fri April 29, 2011 02:38 PM"}]}
来自使用此代码的服务器:
NSArray *sessionsArray = [dictionary objectForKey:@"sessions"];
NSArray *tempArray = [[NSArray alloc] init];
for(NSString *item in sessionsArray){
NSLog(@"Session Found: \'%@\'",item);
NSDictionary *myDictionary = [item JSONValue];
}
我从 JSON 中获得了一个很好的数组,但是当我尝试将每个部分放入 NSDictionary 中时,它给了我一个异常,并且我 NSLoged它并发现引号从某些键和/或值中被删除,如下所示:
{
active = 1;
"contributor_id" = lance;
"created_at" = "Mon May 02, 2011 03:26 PM";
key = e10e5feeea3425ae213cb4cc;
name = "JSON TEST";
status = active;
type = web;
}
这是 JSON 解析器中的错误吗?或者我在做一些愚蠢的事情?
I'm using the SBJSON parser to parse a JSON response:
{"status":0,"sessions":[{"name":"kldlksdklsdkl","active":false,"status":"saved","type":"web","key":"30228ee71f09b93aaa2d1738","contributor_id":"lance","created_at":"Mon May 02, 2011 02:35 PM","closed_at":"Mon May 02, 2011 02:46 PM"}{"name":"Blahieririe","active":false,"status":"saved","type":"web","key":"dbd2bbcc8681bba6a6532051","contributor_id":"lance","created_at":"Mon May 02, 2011 01:42 PM","closed_at":"Mon May 02, 2011 02:34 PM"},{"name":"Jim","active":false,"status":"saved","type":"web","key":"ec5bcf18356a29bb4490841f","contributor_id":"lance","created_at":"Fri April 29, 2011 02:37 PM","closed_at":"Fri April 29, 2011 02:38 PM"}]}
from a server using this code:
NSArray *sessionsArray = [dictionary objectForKey:@"sessions"];
NSArray *tempArray = [[NSArray alloc] init];
for(NSString *item in sessionsArray){
NSLog(@"Session Found: \'%@\'",item);
NSDictionary *myDictionary = [item JSONValue];
}
I'm getting a nice array from my JSON but when I try to put each piece into an NSDictionary, it gives me an exception and I NSLoged it and discovered that the quotation marks are being removed from some of the keys and or values as seen here:
{
active = 1;
"contributor_id" = lance;
"created_at" = "Mon May 02, 2011 03:26 PM";
key = e10e5feeea3425ae213cb4cc;
name = "JSON TEST";
status = active;
type = web;
}
is it a bug in the JSON parser? or am I doing something stupid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引号没有“被删除”。
在 JSON 中,每个字符串都被引号引起来,但引号本身不是字符串的一部分。例如,如果您编写
相应的会话名称,则不会显示引号。
当您使用
NSLog()
字典或数组时,Cocoa 使用 NeXTSTEP 属性列表格式来表示字典/数组。在此格式中,如果值是简单单词,则引号是可选的。也就是说,您应该按如下方式枚举这些 JSON 数据:
The quotation marks aren’t ‘being removed’.
In JSON, every string is quoted but the quotes themselves are not part of the strings. For instance, if you write
the corresponding session name won’t show up with quotation marks.
When you
NSLog()
a dictionary or an array, Cocoa uses the NeXTSTEP property list format to represent the dictionary/array. In this format, quotes are optional if the values are simple words.That said, you should enumerate those JSON data as follows: