如何在Objective C中将JSON序列化数据转换为NSDictionary?

时间:2016-08-22 15:22:06

标签: ios objective-c json

我已经使用以下方法将数据转换为我的应用程序中的字典,但它显示json对象为零。

正在打印“jsonObject为null”。 “error:nil”是否有任何问题。

- (void)postNonceToServer:(NSString *)paymentMethodNonce {
// Update URL with your server


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];



NSDictionary *parameters = @{@"payment_method_nonce":paymentMethodNonce,@"amount":amountStr,@"tripid":tripIdStr,@"currencycode":currencyStr};

[manager POST:@"http:url.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //NSArray *response = responseObject;
    //response = responseObject;

    NSLog(@"responseObject is %@",responseObject);

    jsonObject=[NSJSONSerialization
                JSONObjectWithData:responseObject
                options:NSJSONReadingMutableLeaves
                error:nil];
    NSLog(@"jsonObject is %@",jsonObject);


    }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {

          NSLog(@"Error: %@", error);
      }];

}

1 个答案:

答案 0 :(得分:0)

在这里,我正在使用AFNetworking 3.0。所以我会在AFNetworking 3.0版本中给你“POST”方法代码。

步骤1: - 创建一个NSObject类,并在.h文件中记下此方法。

+ (void)requestPostUrl: (NSString *)serviceName parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure;

步骤2: - 在.m文件中记下此代码 这里“kBaseURL”表示Web服务URL和“serviceName”服务名称。

+ (void)requestPostUrl: (NSString *)serviceName parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure {

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {

    //Here your data converting in JSON.
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:error];
    NSString *argString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return argString;
}];

NSString *strService = [NSString stringWithFormat:@"%@%@",kBaseURL,serviceName];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
//[SVProgressHUD showWithStatus:@"Please wait..."];
[SVProgressHUD show];
[manager POST:strService parameters:dictParams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {


    if([responseObject isKindOfClass:[NSDictionary class]]) {

        if(success) {
            [SVProgressHUD dismiss];
            success(responseObject);
        }
    }
    else
    {
        NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        if(success) {
            [SVProgressHUD dismiss];
            NSLog(@"POST Response  : %@",response);
            success(response);
        }
    }
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    if(failure) {
        [SVProgressHUD dismiss];
        failure(error);
    }
}];
}