Arrays 在NSURLConnection上返回的JSON数据
我有一个iOS应用程序,用户在使用该应用程序之前必须注册。我已经在故事板中创建了UI,并且正在从UITextfields读取用户详细信息。然后,我将详细信息发送到Register API,该API将返回一个JSON响应。我正在使用NSURLConnection进行通信 以下是我从测试URL收到的响应-仅用于测试目的: {“用户名”:“Hans”,“密码”:“Hans”}Arrays 在NSURLConnection上返回的JSON数据,arrays,json,xcode,nsurlconnection,Arrays,Json,Xcode,Nsurlconnection,我有一个iOS应用程序,用户在使用该应用程序之前必须注册。我已经在故事板中创建了UI,并且正在从UITextfields读取用户详细信息。然后,我将详细信息发送到Register API,该API将返回一个JSON响应。我正在使用NSURLConnection进行通信 以下是我从测试URL收到的响应-仅用于测试目的: {“用户名”:“Hans”,“密码”:“Hans”} - (IBAction)registerButtonClicked:(id)sender { NSMutableURL
- (IBAction)registerButtonClicked:(id)sender
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://myDummyURL/Login.php"]];
// Construct the JSON Data
NSDictionary *stringDataDictionary = @{@"firstname": firstname, @"lastname": lastname, @"email": email, @"password" : password, @"telephone" : telephone};
NSError *error;
NSData *requestBodyData = [NSJSONSerialization dataWithJSONObject:stringDataDictionary options:0 error:&error];
// Specify that it will be a POST request
[request setHTTPMethod:@"POST"];
// Set header fields
[request setValue:@"text/plain" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:requestBodyData];
myNSURLConnection = [NSURLConnection connectionWithRequest:request delegate:self];
// Ensure the connection was created
if (myNSURLConnection)
{
// Initialize the buffer
buffer = [NSMutableData data];
// Start the request
[myNSURLConnection start];
}
}
但是,当我尝试读取密码以确保用户不存在时(同样,仅用于测试目的),返回的密码值为nil
在我的.h文件中,我声明了数据和连接:
@interface RegisterViewController : UIViewController <NSURLConnectionDataDelegate>
{
// Conform to the NSURLConnectionDelegate protocol and declare an instance variable to hold the response data
NSMutableData *buffer;
NSURLConnection *myNSURLConnection;
}
此处创建的连接没有问题
在我的.m文件中,我实现了委托方法,在connectionIDFinishLoading()中,我尝试读取返回的JSON。下面是我使用的代码
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Dispatch off the main queue for JSON processing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSString *jsonString = [[NSJSONSerialization JSONObjectWithData:buffer options:0 error:&error] description];
// Dispatch back to the main queue for UI
dispatch_async(dispatch_get_main_queue(), ^{
// Check for a JSON error
if (!error)
{
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSDictionary *dictionary = [jsonArray objectAtIndex:0];
NSString *test = [dictionary objectForKey:@"password"];
NSLog(@"Test is: %@", test);
}
else
{
NSLog(@"JSON Error: %@", [error localizedDescription]);
}
// Stop animating the Progress HUD
});
});
}
从下面的日志屏幕抓取中,您可以看到返回的jsonString具有值,但jsonArray始终为零。错误内容为:error NSError*domain:@“NSCocoaErrorDomain”-代码:3840 0x00007ff158498be0
提前感谢。您的
jsonString
实际上是NSDictionary
对象-由NSJSONSerialization
创建-您正在寻找,没有数组。在JSON字符串中,大括号{}
表示字典,方括号[]
表示数组
试试这个
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Dispatch off the main queue for JSON processing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:buffer options:0 error:&error];
// Dispatch back to the main queue for UI
dispatch_async(dispatch_get_main_queue(), ^{
// Check for a JSON error
if (!error)
{
NSString *test = [dictionary objectForKey:@"password"];
NSLog(@"Test is: %@", test);
}
else
{
NSLog(@"JSON Error: %@", [error localizedDescription]);
}
// Stop animating the Progress HUD
});
});
}
编辑:我忽略了
NSJSONSerialization
行末尾的description
方法。当然必须删除。您的代码有两个问题:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Dispatch off the main queue for JSON processing
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:buffer options:0 error:&error];
// Dispatch back to the main queue for UI
dispatch_async(dispatch_get_main_queue(), ^{
// Check for a JSON error
if (!error)
{
NSString *test = [dictionary objectForKey:@"password"];
NSLog(@"Test is: %@", test);
}
else
{
NSLog(@"JSON Error: %@", [error localizedDescription]);
}
// Stop animating the Progress HUD
});
});
}
我尝试这样做,并在初始化*字典时收到警告。警告:“不兼容的指针类型使用nsstring类型的表达式初始化nsdictionary”,然后应用程序在以下行崩溃:“nsstring*测试=[dictionary objectForKey:@“password”];”出现错误:-[\uu NSCFString objectForKey:::]:无法识别的选择器发送到实例“您在哪里记录行“JSON String…”。实际上,日志文本是字典的字符串表示形式。或者接收到的JSON字符串是否包含用户名/密码以外的信息?