使用AFNetworking解析JSON文件

安基·斯里瓦斯塔瓦(Ankit Srivastava)

我有一个包含json文件的链接。我的意思是,如果我在Chrome中启动该链接,则会在我的计算机上下载一个扩展名为.json的文件。说的链接是www.foffa.com/sampleList.json我是AFNetworking的新手,不知道如何解析此json,而不必将这个文件写到磁盘上。

我的代码如下所示,我很确定必须为此使用流和所有方法,但是我不确定如何使用。到现在"Request failed: unacceptable content-type: text/plain"为止,我得到一个错误,我猜它期望内容类型为"Content-Type" = "application/octet-stream";

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFHTTPResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/octet-stream"];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"Data retrived");       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];
斯巴洛

更改第二行:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFHTTPResponseSerializer serializer];

对此:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

请注意,您正在使用AFJSONResponseSerializer序列化响应。

更新

从以下注释中的信息更新我的答案,看来您实际上是要从服务器下载JSON文件,然后解析该文件,而不是直接从HTTP响应中解析JSON:

为此,将内容类型更改为text/plain或与下载的file/data解析一起作为StringJSON对象,如下所示:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:nil];
operation.responseSerializer = [AFHTTPResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes =
    [NSSet setWithObjects:@"application/octet-stream", @"text/plain", nil];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  NSError *error;
  id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];
  if (error) {
      NSLog(@"Error: %@", error);
  } else {
      NSLog(@"JSON: %@", json);
  }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                      message:[error localizedDescription]
                                                     delegate:nil
                                            cancelButtonTitle:@"Ok"
                                            otherButtonTitles:nil];
  [alertView show];
}];

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 AFNetworking 解析 JSON 的问题?

来自分类Dev

使用AFNetworking 2.0异常解析JSON

来自分类Dev

如何使用AFNetworking进行JSON解析

来自分类Dev

使用AFNetworking将JSON解析为NSDictionary

来自分类Dev

使用AFNetworking解析XML

来自分类Dev

如何使用AFNetworking解析JSON处理布尔值

来自分类Dev

无法从AFNetworking的responseObject解析JSON

来自分类Dev

从AFNetworking解析JSON-Swift

来自分类Dev

使用BeautifulSoup解析JSON文件

来自分类Dev

使用BeautifulSoup解析JSON文件

来自分类Dev

使用python解析json文件

来自分类Dev

使用JSON.NET解析JSON文件

来自分类Dev

尝试使用 Swifty JSON 解析 json 文件

来自分类Dev

AFNetworking更深入地解析json

来自分类Dev

使用JSON的AFNetworking提供objectForKey

来自分类Dev

使用jQuery解析和搜索JSON文件

来自分类Dev

如何使用GSON解析JSON文件

来自分类Dev

如何使用Javascript解析JSON文件

来自分类Dev

使用sed或perl解析JSON文件

来自分类Dev

使用IronPython 2.5解析JSON文件

来自分类Dev

使用Python从JSON文件解析数据

来自分类Dev

如何使用jQuery解析JSON文件?

来自分类Dev

如何使用JavaScript解析JSON文件?

来自分类Dev

在Django中使用Python解析JSON文件

来自分类Dev

使用Python发出解析JSON文件的问题

来自分类Dev

无法使用Pandas解析JSON文件

来自分类Dev

使用Python从JSON文件解析数据

来自分类Dev

使用Python3解析json文件

来自分类Dev

如何使用jQuery解析JSON文件?