iOS JSON解析Objective-C并传递POST方法参数

时间:2016-08-31 05:11:58

标签: ios objective-c json afnetworking

我想在android中使用volleylibrary解析JSON for login。在登录活动中,通过帖子和响应发送两个参数(用户名,密码),如下所示。

参数:用户名,密码

for success:

{
"status": 1,
"message": "successfully login" ,  
    "result": [
{
    "name": "abc",
    "email": "abc@gmail.com",
    "Img": "http://img.com/img.png"
}      
       ]
}

表示错误:

{
"status": 0,
"message": "email or password doesn’t exist"    
}

1 个答案:

答案 0 :(得分:0)

API调用在iOS中使用AFNetworking和POST方法

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:username forKey:@"username"];
[parameters setObject:password forKey:@"password"];

然后调用方法

- (void)apiRequestForPOSTWithServiceName:(NSString *)serviceName andParameters:(NSDictionary *)parameter withCompletion:(void(^)(id response, BOOL isSuccess))completion
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *url = [NSString stringWithFormat:@"%@%@", BASE_URL, serviceName];
[manager POST:url parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //NSLog(@"JSON: %@", responseObject);

    completion(responseObject,YES);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

    completion(error,NO);
}];
}

需要在从服务器

获取数据后解析响应对象
- (void)parseDataWithDict:(id) responeDict
{
NSMutableDictionary *dict = (NSMutableDictionary *) responeDict;
NSString *status = dict[@“status”];
NSString *message = dict[@“message”];
NSDictionary *dictRes = [responeDict[@"result"] firstObject];  // If the result having more data in array, need to iterate the array
NSString *name = dictRes[@“name”];
NSString *email = dictRes[@“email”];
NSString *Img = dictRes[@“Img”];
// If you have any model object class, then add to it and return the object
}

谢谢......快乐的编码。