Objective c 使用AFHTTPClient解析JSON

Objective c 使用AFHTTPClient解析JSON,objective-c,json,nsdictionary,objective-c-blocks,afnetworking,Objective C,Json,Nsdictionary,Objective C Blocks,Afnetworking,我正在使用AFHTTPClient使用AFNetworking库解析json。我可以验证json是否在客户端块中被解析,并将该数据发送到我的json模型。然而,当我试图从块外部访问json模型时,我没有得到任何数据。如何将解析后的json数据传递给json模型,然后在应用程序的其他地方访问该模型数据 AFHTTPClient子类/单例: #import <Foundation/Foundation.h> #import "AFHTTPClient.h" @interface Jso

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

AFHTTPClient子类/单例:

#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
#导入
#导入“AFHTTPClient.h”
@接口JsonClient:AFHTTPClient
+(JsonClient*)共享客户;
@结束
#导入“JsonClient.h”
#导入“AFJSONRequestOperation.h”
静态NSString*常量kJsonBaseURLString=@”https://alpha-api.app.net/";
@JsonClient的实现
+(JsonClient*)共享客户端{
静态JsonClient*\u sharedClient=nil;
静态调度一次;
一次发送(一次发送)^{
_sharedClient=[[JsonClient alloc]initWithBaseURL:[NSURL URLWithString:kJsonBaseURLString]];
});
返回共享客户;
}
-(id)initWithBaseURL:(NSURL*)url{
self=[super initWithBaseURL:url];
如果(!self){
返回零;
}
[self-registerHTTPOperationClass:[AFJSONRequestOperation class];
[self-setDefaultHeader:@“接受”值:@“应用程序/json”];
回归自我;
}
@结束
JSON模型数据:

#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
#导入
@接口:json:NSObject
@属性(非原子,复制)NSString*createdAt;
@属性(非原子,复制)NSString*userText;
-(id)initWithDictionary:(NSDictionary*)dict;
@结束
#导入“TheJson.h”
@JSON的实现
-(id)initWithDictionary:(NSDictionary*)dict{
self=[super init];
如果(自我){
self.createdAt=[dict objectForKey:@“created_at”];
self.userText=[dict objectForKey:@“text]”;
}
回归自我;
}
@结束
要更新用户界面的ViewController:

#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
#导入
@界面ViewController:UIViewController
@结束
#导入“ViewController.h”
#导入“JsonClient.h”
#导入“TheJson.h”
@界面视图控制器()
@属性(弱)IBUILabel*createdLabel;
@属性(弱)IBUILabel*textLabel;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
}
-(无效)未收到记忆警告{
[超级记忆警告];
}
-(iAction)fetchJsonData:(id)发送方{
[[UIApplication sharedApplication]设置网络活动指示器可见:是];
[[JsonClient sharedClient]getPath:@“stream/0/posts/stream/global”参数:nil
成功:^(AFHTTPRequestOperation*操作,id JSON){
NSArray*postsFromResponse=[JSON valueForKeyPath:@“数据”];
NSDictionary*dictFromArray=postsFromResponse[0];
JSON*jsonObject=[[TheJson alloc]initWithDictionary:dictFromArray];
NSLog(@“createdAt是%@”,jsonObject.createdAt);
NSLog(@“来自用户的文本为%@”,jsonObject.userText);
[自更新界面];
[[UIApplication sharedApplication]设置网络活动指示器可见:否];
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
NSLog(@“错误为%@”,[错误描述]);
}
];
}
-(void)更新接口{
TheJson*TheJson;
[\u createdLabel setText:thejson.createdAt];
[_textlabelsettext:thejson.userText];
}
@结束

您没有将新的jsonObject从块中传递出去,也没有将其存储在任何地方。短期答案是声明
updateInterface
,将jsonObject作为参数

因此,您的
updateInterface
变成了
updateInterface:
类似这样的内容:

- (void)updateInterface:(TheJson*)thejson {
    [_createdLabel setText:thejson.createdAt];
    [_textLabel setText:thejson.userText];
}
[self updateInterface:jsonObject];
…然后在块内,您可以这样调用此方法:

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

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

你介意发布一些示例代码吗?我从人们谈论的例子中学到的东西最多。谢谢。是的,我刚刚添加了一个将对象直接传递到
updateInterface:
方法的示例。希望有帮助。在json模型中创建一个委托并使用它将数据传递给视图控制器怎么样?可以。这类事情听起来比必要的更复杂?也许你可以举个例子来说明你的意思。我只是想知道。我现在会坚持你的方法。如果不符合我的需要,我可以在将来发布另一个问题。谢谢你的帮助!