1

我正在使用 AFNetworking 从 URL 获取 JSON,JSON 被解析并应该被转换为 NSData 格式,然后再将其放入数组中。但是,它似乎不起作用。我认为我很愚蠢,因为它以前有效,但现在-

-(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"
  }

我又试了一次,还是不行。results 是一个声明的 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";
        },
4

2 回答 2

0
    AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                         JSONRequestOperationWithRequest:request
                                         success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
                                             self.results = [json valueForKeyPath:@"data"];
                                             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];
    }
                                         } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                         }];

    [operation start];
于 2013-07-31T05:35:31.357 回答
0

您的 json 似乎没有 keyPath "data",删除 valueForKeyPath:@"data",请将 "NSLog(@"Custom Mosaic: %@", json)" 的结果粘贴到您的问题中。操作是异步的。请求返回数据后调用成功块。请像这样将代码放在成功块中

-(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];
}
于 2013-08-01T06:31:01.413 回答