iphone - 非转义控制字符的间歇性 JSON 解析器失败
<p><p>在 iPhone 应用程序中搜索 twitter 以查找某些文本的功能。代码很简单,我把url字符串传给twitter api,然后解析结果。尽管有一半的时间 JSON 解析器因未转义控制字符“0x0”的错误而失败。代码和完整的错误信息如下。</p>
<pre><code> - (void)grabData:(NSString *)searchTerm {
NSString *urlString = ;
NSURL *url = ;
NSLog(@"Created url:%@",url);
//Setup and start async download
NSURLRequest *request = [ initWithURL:url];
NSURLConnection *connection = [ initWithRequest:request delegate:self];
;
;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Store incoming data into a string
NSString *jsonString = [ initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(@"Did receive some data %@",jsonString);
//Create a dictionary from the JSON string
NSDictionary *results = ;
// Build an Array from the dictionary for easy access to each entry
NSDictionary *tweets = ;
// Loop through each entry in the dictionary
for(NSDictionary *tweet in tweets) {
// Get the user string for the tweet
NSString *tUser = ;
NSLog(@"Tweet from %@",tUser);
}
}
</code></pre>
<p>来自控制台的错误消息,50% 的时间,其他 50% 的时间按预期工作。</p>
<pre><code>2010-12-20 21:22:02.022 TwitterSearch Created url:http://search.twitter.com/search.json?q=red&rpp=25
2010-12-20 21:22:02.361 TwitterSearch -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}"
)
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><code>didReceiveData</code> 方法可以在传递数据时多次调用。所以在那个方法中,你应该简单地将每个传入的 block 附加到一个 NSMutableData 类变量(而不是处理它)。</p>
<p>完整数据的处理应该在<code>connectionDidFinishLoading</code>方法中完成。</p>
<p>错误可能是因为它试图解析部分数据 block 。</p></p>
<p style="font-size: 20px;">关于iphone - 非转义控制字符的间歇性 JSON 解析器失败,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/4495836/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/4495836/
</a>
</p>
页:
[1]