首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用AFHTTPClient解析JSON

用AFHTTPClient解析JSON
EN

Stack Overflow用户
提问于 2013-03-23 04:22:42
回答 1查看 594关注 0票数 0

我使用AFNetworking库通过AFHTTPClient解析json。我可以验证json是否在客户端块中被解析,并将该数据发送到我的json模型。但是,当我尝试从块外部访问json模型时,我没有得到任何数据。如何将解析后的json数据传递给json模型,然后在应用程序中的其他位置访问该模型数据?

AFHTTPClient子类/单例:

代码语言:javascript
复制
#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"

@interface JsonClient : AFHTTPClient

+ (JsonClient *)sharedClient;

@end

#import "JsonClient.h"
#import "AFJSONRequestOperation.h"

static NSString *const kJsonBaseURLString = @"https://alpha-api.app.net/";

@implementation JsonClient

+ (JsonClient *)sharedClient {
    static JsonClient *_sharedClient = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[JsonClient alloc] initWithBaseURL:[NSURL URLWithString:kJsonBaseURLString]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];

    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];

    return self;
}

@end

JSON模型数据:

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface TheJson : NSObject

@property (nonatomic, copy) NSString *createdAt;
@property (nonatomic, copy) NSString *userText;

- (id)initWithDictionary:(NSDictionary *)dict;

@end

#import "TheJson.h"

@implementation TheJson

- (id)initWithDictionary:(NSDictionary *)dict {
    self = [super init];

    if (self) {
        self.createdAt = [dict objectForKey:@"created_at"];
        self.userText = [dict objectForKey:@"text"];
    }

    return self;
}

@end

用于更新用户界面的ViewController:

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

#import "ViewController.h"
#import "JsonClient.h"
#import "TheJson.h"

@interface ViewController ()

@property (weak) IBOutlet UILabel *createdLabel;
@property (weak) IBOutlet UILabel *textLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)fetchJsonData:(id)sender {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    [[JsonClient sharedClient] getPath:@"stream/0/posts/stream/global" parameters:nil
                               success:^(AFHTTPRequestOperation *operation, id JSON) {
                                   NSArray *postsFromResponse = [JSON valueForKeyPath:@"data"];
                                   NSDictionary *dictFromArray = postsFromResponse[0];

                                   TheJson *jsonObject = [[TheJson alloc] initWithDictionary:dictFromArray];
                                   NSLog(@"createdAt is %@", jsonObject.createdAt);
                                   NSLog(@"text from user is %@", jsonObject.userText);

                                   [self updateInterface];
                                   [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

                               } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                   NSLog(@"Error is %@", [error description]);
                               }
     ];
}

- (void)updateInterface {
    TheJson *thejson;
    [_createdLabel setText:thejson.createdAt];
    [_textLabel setText:thejson.userText];
}

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-23 05:09:09

您还没有将新的jsonObject传递到块之外,也没有将它存储在任何地方。短期的答案是声明updateInterface以jsonObject作为参数。

所以你的updateInterface变成了updateInterface:,就像这样:

代码语言:javascript
复制
- (void)updateInterface:(TheJson*)thejson {
    [_createdLabel setText:thejson.createdAt];
    [_textLabel setText:thejson.userText];
}

...and然后在你的代码块中,你像这样调用这个方法:

代码语言:javascript
复制
[self updateInterface:jsonObject];

从长远来看,如果您的应用程序有许多这样的对象,并且/或者需要在任意时间内保留它们,那么您可能需要考虑如何在下载时存储和组织这些对象。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15579237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档