获取我的JSON:
{"username":"example","confirmed_rewards":"5","round_estimate":"0.73605946","total_hashrate":"0","payout_history":"10","round_shares":"85",
"workers":{
"worker.1":{"alive":"0","hashrate":"0"},
"worker.2":{"alive":"0","hashrate":"0"}
}
}我的模型:
#import "JSONModel.h"
@protocol LKCoinFCPoolModel @end
@interface LKCoinFCPoolModel : JSONModel
@property (strong, nonatomic) NSString* username;
@property (strong, nonatomic) NSString* confirmed_rewards;
@property (strong, nonatomic) NSString* round_estimate;
@property (strong, nonatomic) NSString* total_hashrate;
@property (strong, nonatomic) NSString* payout_history;
@property (strong, nonatomic) NSString* round_shares;
@property (strong, nonatomic) NSString<Optional> * ErrorCode;
@end我创建了以下函数,用于获取JSON结构并将其分配给模型。
-(void)viewDidAppear:(BOOL)animated
{
//show loader view
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];
//fetch the feed
LKCoinFCPoolModel* test;
test = [[LKCoinFCPoolModel alloc] initFromURLWithString:@"http://example.com/ap/key"
completion:^(LKCoinFCPoolModel *model, JSONModelError *err) {
//hide the loader view
[HUD hideUIBlockingIndicator];
//json fetched
NSLog(@"user: %@", test.username);
}];}我遇到的问题是,它打印的不是user: example,而是user: (null)。
我不确定我做错了什么,这是我尝试用xcode编写的第一个应用程序(我来自Python/Java背景)。
发布于 2013-12-11 05:57:11
您的回调将在LKCoinFCPoolModel *模型中将解析后的JSON传递给您。事实上,我不认为你可以这样分配test。如果你真的想把它放到测试模型中,你应该在块中分配它。请记住,此块在下载和解析JSON之后异步运行。所以测试在那之前是无效的。
https://stackoverflow.com/questions/20505943
复制相似问题