SBJSON 丢失引号

发布于 2024-11-04 11:17:12 字数 1350 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子 2024-11-11 11:17:12

引号没有“被删除”。

在 JSON 中,每个字符串都被引号引起来,但引号本身不是字符串的一部分。例如,如果您编写

NSArray *sessionsArray = [dictionary objectForKey:@"sessions"];
NSDictionary *session = [sessionArray objectAtIndex:0];
NSString sessionName = [session objectForKey:@"name"];

相应的会话名称,则不会显示引号。

当您使用 NSLog() 字典或数组时,Cocoa 使用 NeXTSTEP 属性列表格式来表示字典/数组。在此格式中,如果值是简单单词,则引号是可选的。

也就是说,您应该按如下方式枚举这些 JSON 数据:

NSString *jsonString = …;
NSDictionary *jsonResult = [jsonString JSONValue];
NSArray *sessionsArray = [jsonResult objectForKey:@"sessions"];
for (NSDictionary *session in sessionArray) {
    NSString *sessionName = [session objectForKey:@"name"];
    BOOL sessionActive = [[session objectForKey:@"active"] boolValue];
    NSString *sessionStatus = [session objectForKey:@"status"];
    …
}

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

NSArray *sessionsArray = [dictionary objectForKey:@"sessions"];
NSDictionary *session = [sessionArray objectAtIndex:0];
NSString sessionName = [session objectForKey:@"name"];

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:

NSString *jsonString = …;
NSDictionary *jsonResult = [jsonString JSONValue];
NSArray *sessionsArray = [jsonResult objectForKey:@"sessions"];
for (NSDictionary *session in sessionArray) {
    NSString *sessionName = [session objectForKey:@"name"];
    BOOL sessionActive = [[session objectForKey:@"active"] boolValue];
    NSString *sessionStatus = [session objectForKey:@"status"];
    …
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文