Ios 从AFN网络转换JSON
我使用AFNetworking从URL获取JSON,JSON被解析并被转换成NSData格式,然后再被放入数组中。然而,它似乎不起作用。我觉得我很愚蠢,因为它以前有效,但现在有效了-Ios 从AFN网络转换JSON,ios,objective-c,json,parsing,afnetworking,Ios,Objective C,Json,Parsing,Afnetworking,我使用AFNetworking从URL获取JSON,JSON被解析并被转换成NSData格式,然后再被放入数组中。然而,它似乎不起作用。我觉得我很愚蠢,因为它以前有效,但现在有效了- -(void)loadFromServer{ NSString *trendsURL = [NSString stringWithFormat:@"http://myurl.com/data.json"]; NSURL *url = [NSURL URLWithString:tr
-(void)loadFromServer{
NSString *trendsURL = [NSString stringWithFormat:@"http://myurl.com/data.json"];
NSURL *url = [NSURL URLWithString:trendsURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
self.results = [json valueForKeyPath:@"data"];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];
NSError *anError = nil;
NSArray *parsedElements = [NSJSONSerialization JSONObjectWithData:elementsData
options:NSJSONReadingAllowFragments
error:&anError];
for (NSDictionary *aModuleDict in parsedElements){
MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict];
[elements addObject:aMosModule];
}
}
数据单独显示在类似集合的视图中,当我从文件而不是服务器访问数据时,此单独视图不会出现问题
-(void)loadFromDisk{
NSString *pathString = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSData *elementsData = [NSData dataWithContentsOfFile:pathString];
NSError *anError = nil;
NSArray *parsedElements = [NSJSONSerialization JSONObjectWithData:elementsData
options:NSJSONReadingAllowFragments
error:&anError];
for (NSDictionary *aModuleDict in parsedElements){
MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict];
[elements addObject:aMosModule];
}
}
获取的JSON如下所示:
{
"imageLink": "http://link.com/image.jpg",
"size": 1,
"title": "Food"
}
我又试了一次,但还是不起作用。结果是一个声明的NSArray
-(void)loadFromServer{
NSString *trendsURL = [NSString stringWithFormat:@"http://url.com/data.json"];
NSURL *url = [NSURL URLWithString:trendsURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
NSLog(@"Custom Mosaic: %@", json);
self.results = [json valueForKeyPath:@"data"];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];
for (NSDictionary *aModuleDict in self.results){
MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict];
[elements addObject:aMosModule];
}
}
我认为问题实际上可能是,由于某种原因,它不会获取AFNetworking操作中解析的数据的结果
-(id)init{
self = [super init];
if (self){
elements = [[NSMutableArray alloc] init];
[self loadFromServer];
}
return self;
}
// WWDC 2012 proposed method
+ (CustomMosDatasource *)sharedInstance {
static CustomMosDatasource *sharedInstance;
if (sharedInstance == nil)
sharedInstance = [CustomMosDatasource new];
return sharedInstance;
}
#pragma mark - MosViewDatasourceProtocol
-(NSArray *)mosElements{
NSArray *retVal = elements;
return retVal;
}
日志的结果就是JSON:
Custom Mosaic: {
data = (
{
imageFilename = "http://distilleryimage6.instagram.com/9969123a6af311e2b6c722000a9d0edd_7.jpg";
size = 1;
title = "Title";
},
您的json似乎没有keyPath数据,请删除valueForKeyPath:@data,请粘贴NSLog@Custom马赛克:%@,你的问题中有json。 该操作是异步的。在请求返回数据后调用success块。请像这样将代码放入成功块
-(void)loadFromServer{
NSString *trendsURL = [NSString stringWithFormat:@"http://url.com/data.json"];
NSURL *url = [NSURL URLWithString:trendsURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
NSLog(@"Custom Mosaic: %@", json);
// your json doesn't have keyPath "data"
self.results = json;
// Codes moved into success block
for (NSDictionary *aModuleDict in self.results){
MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict];
[elements addObject:aMosModule];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];
}
1什么不起作用?错误消息?预期和实际结果2元素数据来自哪里?您的代码中未设置或初始化它。-3 AFJSONRequestOperation异步工作!!抱歉,我添加了更多的细节,出现的问题是集合视图中没有显示任何内容。这是某种形式的解析错误,但我不知道是什么。你的实际代码是什么?在我看来,您缺少请求的异步方法。尝试在success回调下执行for语句。感谢您的回答,但这似乎也不起作用,我已经按照上面添加的方式再次尝试了。但是它真的不想工作。谢谢你的代码,但这似乎也不起作用,JSON是按照AFNetworking提取和正确解析的,它是forNSDictionary。。。这似乎是问题所在。结果如何NSLog@Custom马赛克:%@,我已将其添加到问题本身中。
-(void)loadFromServer{
NSString *trendsURL = [NSString stringWithFormat:@"http://url.com/data.json"];
NSURL *url = [NSURL URLWithString:trendsURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
NSLog(@"Custom Mosaic: %@", json);
// your json doesn't have keyPath "data"
self.results = json;
// Codes moved into success block
for (NSDictionary *aModuleDict in self.results){
MosData *aMosModule = [[MosData alloc] initWithDictionary:aModuleDict];
[elements addObject:aMosModule];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];
}