简体   繁体   English

SBJSON 丢失引号

[英]SBJSON losing quotation Marks

I'm using the SBJSON parser to parse a JSON response:我正在使用 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"}]}

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:我从 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;
}

is it a bug in the JSON parser?这是 JSON 解析器中的错误吗? or am I doing something stupid?还是我在做一些愚蠢的事情?

The quotation marks aren't 'being removed'.引号不是“被删除”。

In JSON, every string is quoted but the quotes themselves are not part of the strings.在 JSON 中,每个字符串都被引用,但引号本身不是字符串的一部分。 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.相应的 session 名称不会带引号显示。

When you NSLog() a dictionary or an array, Cocoa uses the NeXTSTEP property list format to represent the dictionary/array.当您NSLog()字典或数组时,Cocoa 使用 NeXTSTEP 属性列表格式来表示字典/数组。 In this format, quotes are optional if the values are simple words.在这种格式中,如果值是简单的单词,引号是可选的。

That said, you should enumerate those JSON data as follows:也就是说,您应该枚举这些 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"];
    …
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM