未转义的控制字符的间歇性JSON解析器失败 [英] Intermittent JSON parser failure for unescaped control character

查看:35
本文介绍了未转义的控制字符的间歇性JSON解析器失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iPhone应用程序中的功能,该应用程序在Twitter上搜索一些文本.代码非常简单,我将url字符串传递给twitter api,然后解析结果.尽管有一半的时间JSON解析器因未转义的控制字符'0x0'失败而失败.代码和完整的错误消息如下.

Functionality in an iPhone app that searches twitter for some text. The code is very straightforward, I pass the url string to the twitter api, and then parse the result. Although half the time JSON parser fails with errors for Unescaped control character '0x0'. Code and full error message below.

    - (void)grabData:(NSString *)searchTerm {
     NSString *urlString = [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=%@&rpp=25",searchTerm];

     NSURL *url = [NSURL URLWithString:urlString];

     NSLog(@"Created url:%@",url);

     //Setup and start async download
     NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
     [connection release];
     [request release];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

     // Store incoming data into a string
     NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

     //NSLog(@"Did receive some data %@",jsonString);

     //Create a dictionary from the JSON string
     NSDictionary *results = [jsonString JSONValue];

     // Build an Array from the dictionary for easy access to each entry
     NSDictionary *tweets = [results objectForKey:@"results"];

            // Loop through each entry in the dictionary
    for(NSDictionary *tweet in tweets) {
     // Get the user string for the tweet
     NSString *tUser = [tweet objectForKey:@"from_user"];
           NSLog(@"Tweet from %@",tUser);
          }

    }

50%的时间来自控制台的错误消息,另外50%的错误消息按预期方式工作.

Error Message from console, 50% of the time, other 50% it works as expected.

2010-12-20 21:22:02.022 TwitterSearch[47362:207] Created url:http://search.twitter.com/search.json?q=red&rpp=25
2010-12-20 21:22:02.361 TwitterSearch[47362:207] -JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=5 \"Unescaped control character '0x0'\" UserInfo=0x4d6a130 {NSLocalizedDescription=Unescaped control character '0x0'}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: profile_image_url\" UserInfo=0x4d6a200 {NSUnderlyingError=0x4d6a170 \"Unescaped control character '0x0'\", NSLocalizedDescription=Object value expected for key: profile_image_url}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Expected value while parsing array\" UserInfo=0x4d6a240 {NSUnderlyingError=0x4d6a1e0 \"Object value expected for key: profile_image_url\", NSLocalizedDescription=Expected value while parsing array}",
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: results\" UserInfo=0x4d6a310 {NSUnderlyingError=0x4d6a2d0 \"Expected value while parsing array\", NSLocalizedDescription=Object value expected for key: results}"
)

推荐答案

在交付数据时,可以多次调用 didReceiveData 方法.因此,在该方法中,您应该简单地将每个传入的块附加到NSMutableData类变量(而不对其进行处理).

The didReceiveData method can be called multiple times as the data is delivered. So in that method, you should simply append each incoming chunk to an NSMutableData class variable (not process it).

完整数据的处理应通过 connectionDidFinishLoading 方法完成.

The processing of the complete data should be done in the connectionDidFinishLoading method.

该错误可能是由于试图解析部分数据块而发生的.

The error is probably happening because it is trying to parse a partial block of data.

这篇关于未转义的控制字符的间歇性JSON解析器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆