objective-c以json方式向后台传数组,应该怎么办
1个回答
展开全部
{
NSString *newUrl = [[NSString alloc] initWithFormat:@"%@register/json", ABC];
NSURL *target = [[NSURL alloc]initWithString:newUrl];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
username, @"name",
password, @"password",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:target];
client.parameterEncoding = AFJSONParameterEncoding;
[client postPath:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self registerRequestFinished:operation];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self RequestFailed:error];
}];
}这里使用的是AFNetworking这个库,内容传递使用的是Json,至少在Url上是看不出内容来的
顺便提供一段使用系统内置库解析Json的代码:NSDictionary
*result = [NSJSONSerialization JSONObjectWithData:[jsonResult
dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSString *newUrl = [[NSString alloc] initWithFormat:@"%@register/json", ABC];
NSURL *target = [[NSURL alloc]initWithString:newUrl];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
username, @"name",
password, @"password",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:target];
client.parameterEncoding = AFJSONParameterEncoding;
[client postPath:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self registerRequestFinished:operation];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self RequestFailed:error];
}];
}这里使用的是AFNetworking这个库,内容传递使用的是Json,至少在Url上是看不出内容来的
顺便提供一段使用系统内置库解析Json的代码:NSDictionary
*result = [NSJSONSerialization JSONObjectWithData:[jsonResult
dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询