【问题标题】:JSON Data isn't being populated未填充 JSON 数据
【发布时间】:2012-07-31 13:47:47
【问题描述】:

我正在尝试为 iOS 编写一个 Facebook Feed 应用程序,并且我正在尝试使用 JSON 框架,但效果不大。每当我运行我的代码时,我都会收到错误“* Terminating app due to unaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'”。我使用来自 Flickr 的提要作为测试/演示 URL,因为 Facebook URL 是使用访问令牌请求和 appendToString 以编程方式创建的:。

    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 语句来测试数据是否为 ​​nil(感谢海报 Dustin为了这个想法)。现在我从 NSLog 得到一个输出,指出“json:(null)或错误:操作无法完成。(Cocoa 错误 3840。)”,并且 if 语句没有响应。所以我认为在创建 NSDictionary json 时会出现问题。

【问题讨论】:

  • 而不是 NSDictionary 尝试 id 一次并查看结果
  • 我把json改成id,NSLog仍然显示json为(null)。

标签: objective-c ios json


【解决方案1】:

您的网址创建不正确。当您使用无效的 url 调用 dataWithContentsOfURL 时,您将得到一个 nil NSData。 JSON 序列化方法需要一个NSData 对象,但得到nil,所以它抛出NSInvalidArgumentException

您的方法中没有任何内容看起来不正确,您只需要检查您的 URL 是否有效。在尝试执行 JSON 序列化之前检查 data 是否不是nil 是个好主意。

如何检查数据是否为零

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

【讨论】:

  • 如何检查数据是否非零?有没有比在 NSLog 中调用描述更好的方法?当我这样做时,它会严重滞后于我的机器。另外,我已经编辑了我的问题,你能看一下,看看你认为可能有什么问题吗?
  • NSLog 仅在调试时使用;当您发行您的应用程序时,它不包括在内,因此滞后不是问题(尽管由于NSLog 而滞后很奇怪)。正在更新答案。
  • 感谢您的回复。我不敢相信我没有想到这么明显的事情!但是当我添加一个 if 循环发送 NSLog if data == nil 时,它不会触发 NSLog。所以我猜当我创建 NSDictionary json 时会发生错误?我将使用 if 语句测试更新我的问题。
【解决方案2】:

Flickr 用他们的 JSON 做了一些解析器无法处理的坏事:

  • 它们以 'jsonFlickrFeed(' 开头并以 ')' 结尾
    • 这意味着根对象无效
  • 他们错误地转义了单引号......例如。 \'
    • 这是无效的 JSON,会让解析器难过!

对于那些希望处理 Flick 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);
}

【讨论】:

    【解决方案3】:

    我在 StackOverflow 上找到了答案:NSJSONSerialization

    事实证明,Flickr 的 JSON 提要格式不正确,因此数据填充的信息带有前缀,NSJSONSerialization 无法处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      相关资源
      最近更新 更多