iOS AFNetworking & Json 数据

Posted

技术标签:

【中文标题】iOS AFNetworking & Json 数据【英文标题】:iOS AFNetworking & Json Data 【发布时间】:2015-03-10 22:03:00 【问题描述】:

我有一本儿子值的字典,但我似乎无法检索多个值。我是新手,我确信我只是忽略了某些东西或完全错误。

这是我在控制台收到的 json 数据:

`2015-03-10 16:41:46.693 Json[8118:629760] https://outpan-images.s3.amazonaws.com/igq8ev0-9780785265740.jpg
2015-03-10 16:41:46.694 Json[8118:629760] 
    attributes =     
        "Author(s)" = "John Eldredge";
        Format = Paperback;
        "ISBN-10" = 0785265740;
        "Page Count" = 288;
        "Publication Date" = "April 2002";
        Publisher = "Nelson, Thomas, Inc.";
    ;
    barcode = 9780785265740;
    images =     (
        "https://outpan-images.s3.amazonaws.com/igq8ev0-9780785265740.jpg"
    );
    name = "Wild at Heart Field Manual: A Personal Guide to Discover the Secret of Your Masculine Soul";
    "outpan_url" = "http://www.outpan.com/view_product.php?barcode=9780785265740";
    videos =     (
    );

这是我尝试检索数据的代码 - 如果我只要求一件事,我可以获得响应 - 但是,当我尝试获取多个值时,它 sigbrt 我正在尝试获取图片网址、名称和条形码。

#import "ViewController.h"
#import "AFNetworking.h"
static NSString * const BaseURLString =     @"http://www.outpan.com/api/get-product.php?barcode=9780785265740&apikey=bbb053368784fed4f89d37f5fbabcf4";

...

- (IBAction)showJson:(id)sender 



// 1
NSString *string = BaseURLString;
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

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

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 

    // 3
   NSDictionary *dic  = (NSDictionary *)responseObject;
     NSArray *imagesUrl = [dic objectForKey:@"images"];
    NSString *imagesUrlString = [imagesUrl objectAtIndex:0];
      NSLog(@"%@", imagesUrlString);
   NSLog(@"%@", responseObject);

//this crashes - 
 //    NSArray *bookName = [dic objectForKey:@"name"];
  //  NSString *bookNameString = [bookName objectAtIndex:0];
  //    NSLog(@"%@", bookNameString);
  // NSLog(@"%@", responseObject);





failure:^(AFHTTPRequestOperation *operation, NSError *error) 

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

// 5
[operation start];



@end

【问题讨论】:

那么,异常消息和异常堆栈跟踪在哪里?? 【参考方案1】:

这是为我运行的代码。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://www.outpan.com/api/get-product.php?barcode=9780785265740&apikey=2bbb053368784fed4f89d37f5fbabcf4" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 
    NSLog(@"JSON: %@", responseObject);
    NSDictionary *aDict = (NSDictionary*)responseObject;
    NSLog(@" name %@",[aDict objectForKey:@"name"]);
    NSLog(@" images %@",[aDict objectForKey:@"images"]);
 failure:^(AFHTTPRequestOperation *operation, NSError *error) 
    NSLog(@"Error: %@", error);
];

这是我得到的控制台。

2015-03-11 04:39:43.038 del[49323:3297516]  name Wild at Heart Field Manual: A Personal Guide to Discover the Secret of Your Masculine Soul
2015-03-11 04:39:43.038 del[49323:3297516]  images (
    "https://outpan-images.s3.amazonaws.com/igq8ev0-9780785265740.jpg"
)

【讨论】:

谢谢!我一直都有这些信息,只是不知道如何访问它。 @ClayD 很高兴能帮上忙。快乐编码。 :D

以上是关于iOS AFNetworking & Json 数据的主要内容,如果未能解决你的问题,请参考以下文章

iOS-三方框架AFNetworking基本使用

iOS_SN_基于AFNetworking3.0网络封装

如何与 Heroku 和 iOS 一起做 AFNetworking?

如何将 AFNetworking 添加到 XCode 应用程序

AFNetworking 仅在请求成功时下载和保存数据

ios基础篇(三十)—— AFNetworking的使用