Objective-C 中 SBJson 输入意外结束

发布于 2024-11-27 19:15:02 字数 2402 浏览 3 评论 0原文

我有以下代码试图解析我从网站返回的 JSON:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

     [connection release];

     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

     NSLog(@"my ns string = %@", responseString);

     [responseData release];

     NSError *error; 
     NSDictionary *dictionary = [parser objectWithString:responseString   
                                               error:&error]; 
     while (error) { 
         NSLog(@"%@", error); 
         error = [[error userInfo] objectForKey:NSUnderlyingErrorKey]; 
     } 

     NSLog(@"%@", dictionary);


 }

我收到这些错误:

2011-08-01 20:16:47.273 myJSONParser[1040:b603] Connection didReceiveResponse: 
<NSHTTPURLResponse: 0x4e810f0> - application/json
2011-08-01 20:16:47.279 myJSONParser[1040:b603] Connection didReceiveData of length: 117
2011-08-01 20:16:47.359 myJSONParser[1040:b603] my ns string = 
2011-08-01 20:16:47.361 myJSONParser[1040:b603] Error Domain=org.brautaset.SBJsonParser.ErrorDomain Code=0 "Unexpected end of input" UserInfo=0x4eada30 {NSLocalizedDescription=Unexpected end of input}
2011-08-01 20:16:47.363 myJSONParser[1040:b603] (null)

我确实可以控制我尝试获取的 JSON,并且在生成 JSON 的 PHP 内部设置了标头像这样:

header("Content-Type:application/json");
...
echo json_encode($arr);

“输入意外结束”让我相信 JSON 在某种程度上是错误的,但 JSONLint 告诉我它完全有效。我做错了什么?感谢您花时间阅读本文,任何建议将不胜感激!

更新:

我像这样设置responseData:

NSMutableData *responseData;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
         NSLog(@"Connection didReceiveData of length: %u", data.length);

         [responseData appendData:data];

     }

解析器像这样(在.h中):

SBJsonParser *parser;

和.m:

parser = [[SBJsonParser alloc] init];

查尔斯调查结果:

HTTP/1.1 200 OK
Date: Tue, 02 Aug 2011 03:30:14 GMT
Server: Apache
X-Powered-By: PHP/5.2.15
Transfer-Encoding: chunked
Content-Type: application/json

[{"key1":"Name","key2":"First","key3":"2011-08-13"},{"key1":"Second","key2":"test2","key3":"2011-08-13"}]

收到的NSData的描述:

打印数据描述:{length = 117,容量= 256,字节= 0x5b7b22686f6d65223a22426c61636b62 ... 30382d3133227d5d}

I have the following code that is trying to parse a JSON that I am returning from a website:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

     [connection release];

     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

     NSLog(@"my ns string = %@", responseString);

     [responseData release];

     NSError *error; 
     NSDictionary *dictionary = [parser objectWithString:responseString   
                                               error:&error]; 
     while (error) { 
         NSLog(@"%@", error); 
         error = [[error userInfo] objectForKey:NSUnderlyingErrorKey]; 
     } 

     NSLog(@"%@", dictionary);


 }

I get these errors:

2011-08-01 20:16:47.273 myJSONParser[1040:b603] Connection didReceiveResponse: 
<NSHTTPURLResponse: 0x4e810f0> - application/json
2011-08-01 20:16:47.279 myJSONParser[1040:b603] Connection didReceiveData of length: 117
2011-08-01 20:16:47.359 myJSONParser[1040:b603] my ns string = 
2011-08-01 20:16:47.361 myJSONParser[1040:b603] Error Domain=org.brautaset.SBJsonParser.ErrorDomain Code=0 "Unexpected end of input" UserInfo=0x4eada30 {NSLocalizedDescription=Unexpected end of input}
2011-08-01 20:16:47.363 myJSONParser[1040:b603] (null)

I do have control over the JSON that I am trying to acquire and inside the PHP that generates the JSON I set the header like this:

header("Content-Type:application/json");
...
echo json_encode($arr);

"Unexpected end of input" leads me to believe that the JSON is somehow malformed, but JSONLint tells me its perfectly valid. What am I doing wrong? Thank you for taking the time to read through this, any advice would really be appreciated!

UPDATE:

I set the responseData like this:

NSMutableData *responseData;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
         NSLog(@"Connection didReceiveData of length: %u", data.length);

         [responseData appendData:data];

     }

And the parser like this (in .h):

SBJsonParser *parser;

And .m:

parser = [[SBJsonParser alloc] init];

Charles Findings:

HTTP/1.1 200 OK
Date: Tue, 02 Aug 2011 03:30:14 GMT
Server: Apache
X-Powered-By: PHP/5.2.15
Transfer-Encoding: chunked
Content-Type: application/json

[{"key1":"Name","key2":"First","key3":"2011-08-13"},{"key1":"Second","key2":"test2","key3":"2011-08-13"}]

Description of the NSData received:

Printing description of data: {length =
117, capacity = 256, bytes = 0x5b7b22686f6d65223a22426c61636b62 ...
30382d3133227d5d}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

江挽川 2024-12-04 19:15:02

代码来看,我们只得到“意外的输入结束” " 当找到 EOF 令牌时。

SBJsonParser.m:

case SBJsonStreamParserWaitingForData:
            self.error = @"Unexpected end of input";
            break;

SBJsonStreamParserState.m:

- (SBJsonStreamParserStatus)parserShouldReturn:(SBJsonStreamParser*)parser {
    return SBJsonStreamParserWaitingForData;
}

SBJsonStreamParser.m:

    switch (tok) {
        case sbjson_token_eof:
            return [state parserShouldReturn:self];
            break;
    ... }

您的日志似乎表明 NSString 有问题。我会检查响应数据以确保其正确——您可以使用 Fiddler 或 Charles 等代理、xcode 调试器或普通的旧 NSLog() 来完成此操作。

编辑:

好的,我将从调试器获得的数据转换为 UTF-8,这就是我得到的:

[{"home":"Blackb ... 08-13"}]

您只粘贴了字节数组的部分位,因此省略号所在的位置应该有更多。 “家”或“黑”对你来说有什么意义吗?它不会出现在 Charles 截获的响应数据中的任何位置。您是否在 didReceiveReponse 委托方法中对responseData 执行了 setLength:0 操作?

From the code, it looks like we only get "unexpected end of input" when the EOF token is found.

SBJsonParser.m:

case SBJsonStreamParserWaitingForData:
            self.error = @"Unexpected end of input";
            break;

SBJsonStreamParserState.m:

- (SBJsonStreamParserStatus)parserShouldReturn:(SBJsonStreamParser*)parser {
    return SBJsonStreamParserWaitingForData;
}

SBJsonStreamParser.m:

    switch (tok) {
        case sbjson_token_eof:
            return [state parserShouldReturn:self];
            break;
    ... }

Your logs seem to indicate that something is wrong with the NSString. I would examine the response data to make sure it's correct--you can do this with a proxy like Fiddler or Charles, the xcode debugger, or just plain old NSLog().

Edit:

Ok I converted the data you got from the debugger to UTF-8 and this is what I got:

[{"home":"Blackb ... 08-13"}]

You only pasted a partial bit of the byte array, so there should be more where the ellipsis are. Does the 'home' or 'blackb' mean anything to you? It wan't anywhere in the response data that Charles intercepted. Do you do a setLength:0 on responseData in the didReceiveReponse delegate method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文