iOS 9解析JSON的最佳解决方案

时间:2015-12-15 05:09:49

标签: ios objective-c json

当我需要解析Feed JSON时,我总是使用此解决方案。

https://stackoverflow.com/a/20077594/2829111

sendAsynchronousRequest现已弃用,我仍然坚持使用此代码

__block NSDictionary *json;    
[[session dataTaskWithURL:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        // handle response
        json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"Async JSON: %@", json);
        [collectionView reloadData];
}] resume];

这样,reloadData参数需要很长时间才能执行。我已经尝试用以下方法强制回到主队列:

__block NSDictionary *json;    
[[session dataTaskWithURL:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            // handle response
            json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"Async JSON: %@", json);
            dispatch_sync(dispatch_queue_create("com.foo.samplequeue", NULL), ^{[collectionView reloadData});
}] resume];

3 个答案:

答案 0 :(得分:1)

问题是完成处理程序不在主队列上运行。但是所有UI更新必须在主队列上进行。所以将它发送到主队列:

{{1}}

答案 1 :(得分:0)

为什么不试试JSONModel库.......它使用起来非常简单

-(void)getEmployeePerformance:(EmpPerformanceRequest*)request
              withSuccesBlock:(succesEmployeePerformanceResponseBlock) successBlock
                 andFailBlock:(FailResponseBlock) failBlock
{       
    NSString* weatherUrl    = [[ABWebServiceUtil sharedInstance]getEmployeePerformanceURL];
    [HTTPClientUtil postDataToWS:weatherUrl parameters:[request toDictionary] WithHeaderDict:nil withBlock:^(AFHTTPRequestOperation *responseObj, NSError *error)
     {

        if(responseObj.response.statusCode == HTTP_RESPONSE_SUCESS)
        {
            EmpPerformanceArrayModel *empPerfArrModel;
           if(responseObj.responseString)
           {
               empPerfArrModel                  = [[EmpPerformanceArrayModel alloc]initWithString:result error:nil];
               empPerfArrModel.employeesArray   = [empPerformanceModel arrayOfModelsFromDictionaries:empPerfArrModel.employeesArray];
           }
            if(successBlock)
            {
                successBlock(responseObj.response.statusCode, empPerfArrModel);
            }
        }else if (failBlock)
        {
            failBlock(responseObj.response.statusCode);
        }
     }];
}

有关详细信息,请点击此链接......它会向您简要介绍

https://github.com/icanzilb/JSONModel

答案 2 :(得分:-1)

尝试在connectionDidFinishLoading中解析JSON,这样您就可以获得NSDictionary的响应。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
Class NSJSONSerializationclass = NSClassFromString(@"NSJSONSerialization");
    NSDictionary *result;
    NSError *error;

    if (NSJSONSerializationclass)
    {
        result = [NSJSONSerialization JSONObjectWithData: responseData options: NSJSONReadingMutableContainers error: &error];
    }

    // If the webservice response having values we have to call the completionBlock…
    if (result)
    {
        if (self.completionBlock != nil)
        {
            self.completionBlock(result);
        }
    }
}