2012-07-31 170 views
-1

我正在尝试为iOS编写一个Facebook Feed应用程序,并且我试图使用JSON框架,效果很差。每当我运行我的代码,我得到错误“*由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'数据参数为零'”。我使用Flickr的馈送作为测试/演示网址,因为Facebook网址是使用访问令牌请求和appendToString:以编程方式创建的。JSON数据未被填充

NSURL *url2 = [NSURL URLWithString:@"www.flickr.com/services/feeds 
        /photos_public.gne?tags=punctuation&someKey=atsign&format=json"]; 
    NSError *error = nil; 
    NSData *data = [NSData dataWithContentsOfURL:url2]; 
    if (data == nil){ 
     NSLog(@"data is nil"); 
    } 
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data 
                 options:NSJSONReadingMutableContainers 
                  error:nil]; 
    NSLog(@"json: %@\n\n Or Error: %@", json, [error localizedDescription]); 

编辑:我改变了在我的代码,包括错误和NSLog的,并且改变了URL(在海报ILIS的意见),以及if语句来测试,如果数据是零(添加感谢海报达斯汀的想法)。现在我从NSLog得到一个输出,它声明“json:(null)或错误:操作无法完成(可可错误3840.)”,并且if语句没有响应。所以我认为当NSDictionary json被创建时,问题就出现了。

+0

而非NSDictionary的尝试ID一次,看看结果 – ilhnctn 2012-07-31 17:59:35

+0

我改变JSON来标识和的NSLog仍显示JSON作为(空)。 – Chance 2012-07-31 19:57:28

回答

0

我发现在计算器上答案:NSJSONSerialization

原来,Flickr的JSON供稿格式不正确,因此数据中填充了前缀无法通过NSJSONSerialization处理的信息。

1

您的网址被错误地创建。当您拨打dataWithContentsOfURL的网址无效时,您会收到nilNSData。 JSON序列化方法需要一个NSData对象,但获得nil,因此它会抛出NSInvalidArgumentException

你的方法没有看起来不正确,你只需要检查你的URL是否有效。在尝试执行JSON序列化之前,检查data是否为非nil是一个好主意。

如何检查数据是否为零

if (data == nil) 
{ 
    //handle the problem 
} 
else 
{ 
    //You have valid content, do something with it 
} 
+0

如何检查数据是否为零?有没有比在NSLog中调用描述更好的方法?当我这样做时,它滞后于我的机器很糟糕。另外,我编辑了我的问题,你可以看一看,看看你认为可能是错的吗? – Chance 2012-07-31 16:09:23

+0

'NSLog'只能在调试过程中使用;当你发布你的应用程序时,它不包括在内,所以滞后不是问题(虽然你因为NSLog而滞后很奇怪)。更新答案。 – Dustin 2012-07-31 16:18:01

+0

感谢您的回复。我无法相信我没有想到明显的事情!但是当我添加一个if循环发送一个NSLog数据== nil时,它不会触发NSLog。所以我猜这个错误发生在创建NSDictionary json时?我会用if语句测试更新我的问题。 – Chance 2012-07-31 16:25:27

1

的Flickr确实与他们的JSON一些不好的事情分析器无法应付:

  • 他们开始与“jsonFlickrFeed(”和结束“)”
    • 这意味着根对象无效
  • 他们不正确地转义单引号..例如。 \'
    • 这是无效的JSON,并会使解析器伤心!

对于那些希望处理弗里克JSON提要

//get the feed 
NSURL *flickrFeedURL = [NSURL URLWithString:@"http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=data"]; 
NSData *badJSON = [NSData dataWithContentsOfURL:flickrFeedURL]; 
//convert to UTF8 encoded string so that we can manipulate the 'badness' out of Flickr's feed 
NSString *dataAsString = [NSString stringWithUTF8String:[badJSON bytes]]; 
//remove the leading 'jsonFlickrFeed(' and trailing ')' from the response data so we are left with a dictionary root object 
NSString *correctedJSONString = [NSString stringWithString:[dataAsString substringWithRange:NSMakeRange (15, dataAsString.length-15-1)]]; 
//Flickr incorrectly tries to escape single quotes - this is invalid JSON (see http://stackoverflow.com/a/2275428/423565) 
//correct by removing escape slash (note NSString also uses \ as escape character - thus we need to use \\) 
correctedJSONString = [correctedJSONString stringByReplacingOccurrencesOfString:@"\\'" withString:@"'"]; 
//re-encode the now correct string representation of JSON back to a NSData object which can be parsed by NSJSONSerialization 
NSData *correctedData = [correctedJSONString dataUsingEncoding:NSUTF8StringEncoding]; 
NSError *error = nil; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:correctedData options:NSJSONReadingAllowFragments error:&error]; 
if (error) { 
    NSLog(@"this still sucks - and we failed"); 
} else { 
    NSLog(@"we successfully parsed the flickr 'JSON' feed: %@", json); 
}