iPhone 无法解析来自 YELP 的 JSON 结果

发布于 2024-10-20 21:28:03 字数 1147 浏览 1 评论 0原文

我正在尝试从 yelp 搜索结果中解析经过验证的 JSON。

这会按预期正确地输出 json(在模拟器浏览器和我自己的浏览器中得到确认)。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSString *dump = [[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding] autorelease];
    
    NSLog(@"Did Recieve data: %@",  dump);
    [JSONData appendData:data];
}

但是当我的连接完成加载时,我很难提取结果并解析数据:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{   
    NSLog(@"Connection Did Finish Loading");
    
    NSError *error = nil;
    id cureLocations = [[CJSONDeserializer deserializer] deserializeAsDictionary:JSONData error:&error];
    [JSONData release];
        
    NSLog(@"Connection finished loading: %@", error);
}

我得到:连接完成加载:Error Domain = CJSONDeserializerErrorDomain Code = -11“操作无法完成。(CJSONDeserializerErrorDomain错误-11) .)”

我从 SBJSON 切换到 TouchJSON,因为我也无法从该框架中提取它。我尝试将其加载到字典和数组中,结果为 null。此时我已经在键盘上敲击了几个小时,并且非常感谢任何输入。

JSON 示例

更新:

我是一个假人。我没有初始化 JSONData。请接受我对浪费您的时间的歉意,并感谢您的建议。

I'm attempting to parse validated JSON from a yelp search result.

This correctly spits out the json as expected (confirmed in simulator browser and my own).

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSString *dump = [[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding] autorelease];
    
    NSLog(@"Did Recieve data: %@",  dump);
    [JSONData appendData:data];
}

But when my connection finishes loading I'm having a hard time extracting the results and parsing the data:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{   
    NSLog(@"Connection Did Finish Loading");
    
    NSError *error = nil;
    id cureLocations = [[CJSONDeserializer deserializer] deserializeAsDictionary:JSONData error:&error];
    [JSONData release];
        
    NSLog(@"Connection finished loading: %@", error);
}

I get: Connection finished loading: Error Domain=CJSONDeserializerErrorDomain Code=-11 "The operation couldn’t be completed. (CJSONDeserializerErrorDomain error -11.)"

I switched to TouchJSON from SBJSON because I wasn't able to extract it from that framework either. I've attempted loading it into Dictionaries and Arrays with null as the result. At this point I've been banging my head on the keyboard for hours and would greatly appreciate any input.

JSON sample

Update:

I am a dummy. I hadn't initialized JSONData. Please accept my apologies for wasting your time and thanks for your suggestions.

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

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

发布评论

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

评论(2

Bonjour°[大白 2024-10-27 21:28:04

SBJSON 是一个相当不错且众所周知的解析器。如果它没有解析您的输入,您可能会认为这是因为输入确实很糟糕。如果 TouchJSON 也不解析它,则输入肯定是错误的。因此,您的 JSONData 对象发生了一些不可靠的事情。

我建议您在 connectionDidFinishLoading 方法中将 JSON 数据打印到控制台,并尝试重新验证它。查看您传递给 CJSON 的数据对象中实际包含的内容。

SBJSON is a pretty decent and well known parser. If it didn't parse your input, you'd probably assume it's because the input was genuinely bad. If TouchJSON isn't parsing it either, the input is definitely bad. So there's something going on with you JSONData object that's dodgy.

I would suggest you print out your JSON data to the console in your connectionDidFinishLoading method and try re-validating it. See what's actually in the data object you're passing to CJSON.

纸伞微斜 2024-10-27 21:28:04

呃,在进一步审查该应用程序后,我似乎急于将示例复制到该项目中,并忘记初始化 JSONData:

self.JSONData = [[[NSMutableData alloc] init]autorelease];

然后我更新了我的 didReceiveData 方法:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [[self JSONData] appendData:data];
}

现在一切都按预期工作。这是我第二次遇到这个错误。我想我一直希望调试器能够找到它。感谢大家的时间和帮助。

Ugh, after further review of the application it seems that I rushed to copy my samples into this project and forgot to initialize JSONData:

self.JSONData = [[[NSMutableData alloc] init]autorelease];

Then I updated my didReceiveData method:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [[self JSONData] appendData:data];
}

And everything is now working as expected. This is the second time I've run into this error. I guess I always expected the debugger to pick it up. Thanks for everyones time and assistance.

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