如何将php返回NSData转换为NSDictionary [英] how to convert php return NSData into NSDictionary
问题描述
HI,
我正在为我的iphone应用程序创建登录屏幕。我使用NSURLConnection方法将用户名和密码发送到php。我可以成功发送登录详细信息到PHP。我的php页面根据登录详细信息返回状态值。
i am creating an login screen to my iphone application. i am sending the username and password to php using NSURLConnection method. i can successfully send login details to php. My php page returning status values based on the login details.
(status = OK / Fail,redirectionUrl = http://www.balalaa.com )
(status =OK / Fail , redirectionUrl=http://www.balalaa.com)
在DidReceiveData方法中,如果我转换NSData成为字符串我正在获取成功登录的字符串
In DidReceiveData method if i convert the NSData into string i am getting following string for successful login
STATUS = OK& url = http://www.balalaa.com 。
"STATUS=OK&url=http://www.balalaa.com".
有什么方法可以获取STATUS和url的值,不使用NSPredict。有没有其他方法可以将NSData转换为NSDictionary来获取STATUS键的值?
Is there any way to fetch the values of STATUS and url, without using NSPredict. Is there any other way to conert the NSData into NSDictionary to fetch values for STATUS key?
提前致谢。
Ram
Thanks in advance. Ram
推荐答案
NSString *urlDataString = //Whatever data was returned from the server as an NSString
NSArray *parameters = [urlDataString componentsSeparatedByString:@"&"];
NSMutableDictionary *parameterDictionary = [NSMutableDictionary dictionary];
for (NSString *parameter in parameters) {
NSArray *parameterComponents = [parameter componentsSeparatedByString:@"="];
[parameterDictionary setObject:[parameterComponents objectAtIndex:1]
forKey:[parameterComponents objectAtIndex:0]];
}
另一方面,NSScanner将为您提供更有效的方式这一点,无论哪种方式,您都必须解除从字典中获得的任何值和密钥。
On the other hand, NSScanner will give you a much more efficient way of doing this, and either way you will have to un-escape whatever values and keys you get from the dictionary.
这篇关于如何将php返回NSData转换为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!