iOS JSON解析Objective-C并传递POST方法参数
Posted
技术标签:
【中文标题】iOS JSON解析Objective-C并传递POST方法参数【英文标题】:iOS JSON Parsing Objective-C and pass POST method parameter 【发布时间】:2016-08-31 05:11:57 【问题描述】:我想使用 volleylibrary 解析 android 中的 JSON 进行登录。在登录活动中,通过 post 和响应发送两个参数(用户名、密码),如下所示。
参数:用户名、密码
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"
【问题讨论】:
android me krna hai ya ios me? (你想在 iOS 或 Android 上做吗?) 你能出示你的代码吗 【参考方案1】: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
谢谢...愉快的编码。
【讨论】:
以上是关于iOS JSON解析Objective-C并传递POST方法参数的主要内容,如果未能解决你的问题,请参考以下文章