Afnetworking返回null json虽然它有效

时间:2015-03-06 20:11:05

标签: ios objective-c json afnetworking

我想使用Afnetworking解析json,但它返回null虽然json有效且在我的代码中运行良好

 -(void)getData:(NSString *)serverName
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"Loading";




    [manager POST:serverName parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {





        [hud hide:YES];
        Category = [[NSMutableArray alloc]init];

        NSLog(@"%@",operation.responseString);

        // 3
        Category = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];


        [self.myCollectionView reloadData];



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error %@", error);
        [hud hide:YES];

        // 3


        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"خطأ في الاتصال" message:@"توجد مشكلة في الاتصال قم بالمحاولة مرة اخرى" delegate:self cancelButtonTitle:@"اغلاق" otherButtonTitles:nil];
        [alert show];

    }];


}

但是当我在控制台上显示数组时,它返回null 虽然当我显示operation.responseString 它输出真正的json

[{"cat_name":"الكترونيات","cat_img":"الكترونيات.jpg"},{"cat_name":"arabiat","cat_img":"arabypic.jpg"}]

responseObject的输出是

<efbbbf0d 0aefbbbf 5b7b2263 61745f6e 616d6522 3a22d8a7 d984d983 d8aad8b1 d988d986 d98ad8a7 d8aa222c 22636174 5f696d67 223a22d8 a7d984d9 83d8aad8 b1d988d9 86d98ad8 a7d8aa2e 6a706722 7d2c7b22 6361745f 6e616d65 223a2261 72616269 6174222c 22636174 5f696d67 223a2261 72616279 7069632e 6a706722 7d5d0a>

1 个答案:

答案 0 :(得分:0)

您应该将管理器响应序列化程序设置为JSON,以便AFN自动解码数据。完成后,您无需使用NSJSONSerialization,只需使用将成为您阵列的responseObject