iOS 5 Json 序列化

标签 ios json

我已经使用 JSON 序列化来获取 json 响应,在这里我一切都很好,但是当我需要将一些值作为键值对与 URL 一起发布时。我已经这样做了,但没有得到结果。

NSArray *objects = [NSArray arrayWithObjects:@"uname", @"pwd", @"req",nil];
NSArray *keys = [NSArray arrayWithObjects:@"ann", @"ann", @"login", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:keys forKeys:objects];


if ([NSJSONSerialization isValidJSONObject:dict]) {
    NSError *error;

    result = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

    if (error == nil && result != nil) {
       // NSLog(@"Success");
    }
}

NSURL * url =[NSURL URLWithString:@"URL_address_VALUE/index.php"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[result length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:result];

NSURLResponse *res = nil;
NSError *error = nil;

NSData *ans = [NSURLConnection sendSynchronousRequest:request returningResponse:&res error:&error];

if (error == nil) {

    NSString *strData = [[NSString alloc]initWithData:ans encoding:NSUTF8StringEncoding];

    NSLog(@"%@",strData);
}

我不知道这里出了什么问题...请伙计们帮助我..

最佳答案

您的代码中有多个错误,请使用我的代码作为引用并将其与您的代码进行比较,您将获得由您完成的错误。
从 Objective-C 的角度来看,以下代码可以正常工作。您的 URL 或服务端存在一些错误。

工作代码:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"ann",@"uname",@"ann",@"pwd",@"login",@"req", nil];
NSLog(@"dict :: %@",dict);
NSError *error2;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error2];
NSString *post = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"postLength :: %@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://exemplarr-itsolutions.com/dbook/index.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *error3;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error3];
NSString *str = [[NSString alloc] initWithData:POSTReply encoding:NSUTF8StringEncoding];
NSLog(@"str :: %@",str);

关于iOS 5 Json 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17045072/

相关文章:

ios - 在 iOS 6 中没有调用 shouldAutorotateToInterfaceOrientation

ios - 如何将 View 保存为图像

iphone - containsObject : and member: methods of NSSet? 之间的区别

python - 如何自定义排序要在 json.dumps 中使用的字典列表

Python:位请求

javascript - 如何检索 Facebook 用户信息

ios - 文本字段验证。验证第一个字符。 iOS

ios - 无法在 Storyboard上的 SWRevealViewController 中设置委托(delegate)

java - Marshall 具有 OnetoMany 关系的 hibernate pojo 类

python - 如何使用 Jinja2 模板引擎生成 json