[英]ios parsing json result after http request
am starting to build login form reading from external server via http request i need to parse json result to get user name 正在开始建立通过http请求从外部服务器读取的登录表单,我需要解析json结果以获取用户名
- (IBAction)getlogin:(UIButton *)sender {
NSString *rawStrusername = [NSString stringWithFormat:@"username=%@",_username.text];
NSString *rawStrpassword = [NSString stringWithFormat:@"password=%@",_password.text];
NSString *post = [NSString stringWithFormat:@"%@&%@", rawStrusername, rawStrpassword];
// NSString *post = @"rawStrusername&rawStrpassword";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
/* NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; */
NSURL *url = [NSURL URLWithString:@"http://www.othaimmarkets.com/my_services_path/user/login.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
/* [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; */
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
//NSLog(@"responseData: %@", responseData);
}
I get this result: 我得到这个结果:
{"sessid":"g2ev7til6d750ducrkege0cbj2","session_name":"SESS02795057fe9e6b2fc0777bf4057b248f","user":{"uid":"617","name":"mohammed.abdelrasoul@gmail.com","mail":"mohammed.abdelrasoul@gmail.com","mode":"0","sort":"0","threshold":"0","theme":"","signature":"","signature_format":"0","created":"1316602317","access":"1352643854","login":"1352666338","status":"1","timezone":"10800","language":"ar","picture":"","init":"mohammed.abdelrasoul@gmail.com","data":"a:5:{s:18:\\"country_iso_code_2\\";s:2:\\"SA\\";s:13:\\"timezone_name\\";s:11:\\"Asia/Riyadh\\";s:5:\\"block\\";a:1:{s:7:\\"webform\\";a:1:{s:15:\\"client-block-88\\";i:1;}}s:13:\\"form_build_id\\";s:37:\\"form-3ae73833f08accc7abe5517347ea87eb\\";s:7:\\"contact\\";i:0;}","country_iso_code_2":"SA","timezone_name":"Asia/Riyadh","block":{"webform":{"client-block-88":1}},"form_build_id":"form-3ae73833f08accc7abe5517347ea87eb","contact":0,"roles":{"2":"authenticated user"}}}
{“ sessid”:“ g2ev7til6d750ducrkege0cbj2”,“ session_name”:“ SESS02795057fe9e6b2fc0777bf4057b248f”,“ user”:{“ uid”:“ 617”,“ name”:“ mohammed.abdelrasoul@gmail.com”,“ mail”:“ mohammed.abdelrasoul@gmail.com” .abdelrasoul @ gmail.com“,” mode“:” 0“,” sort“:” 0“,” threshold“:” 0“,” theme“:”“,” signature“:”“,” signature_format“: “ 0”,“创建”:“ 1316602317”,“访问”:“ 1352643854”,“登录”:“ 1352666338”,“状态”:“ 1”,“时区”:“ 10800”,“语言”:“ ar “,”图片“:”“,” init“:” mohammed.abdelrasoul@gmail.com“,”数据“:” a:5:{s:18:\\“ country_iso_code_2 \\”; s:2:\\“ SA \\“; s:13:\\” timezone_name \\“; s:11:\\” Asia / Riyadh \\“; s:5:\\” block \\“; a:1:{s:7:\\” webform \\“; a:1:{s:15:\\“ client-block-88 \\”; i:1;}} s:13:\\“ form_build_id \\”; s:37:\\“ form-3ae73833f08accc7abe5517347ea87eb \\”; s:7 :\\“ contact \\”; i:0;}“,” country_iso_code_2“:” SA“,” timezone_name“:” Asia / Riyadh“,” block“:{” webform“:{” client-block-88“: 1}},“ form_build_id”:“ form-3ae73833f08accc7abe5517347ea87eb”,“联系人”:0,“角色”:{“ 2”:“经过身份验证的用户”}}}
Or, formatted for the sake of legibility: 或者,为了清晰起见,对其进行了格式化:
{
"sessid":"g2ev7til6d750ducrkege0cbj2",
"session_name":"SESS02795057fe9e6b2fc0777bf4057b248f",
"user":{
"uid":"617",
"name":"mohammed.abdelrasoul@gmail.com",
"mail":"mohammed.abdelrasoul@gmail.com",
"mode":"0",
"sort":"0",
"threshold":"0",
"theme":"",
"signature":"",
"signature_format":"0",
"created":"1316602317",
"access":"1352643854",
"login":"1352666338",
"status":"1",
"timezone":"10800",
"language":"ar",
"picture":"",
"init":"mohammed.abdelrasoul@gmail.com",
"data":"a:5:{s:18:\"country_iso_code_2\";s:2:\"SA\";s:13:\"timezone_name\";s:11:\"Asia/Riyadh\";s:5:\"block\";a:1:{s:7:\"webform\";a:1:{s:15:\"client-block-88\";i:1;}}s:13:\"form_build_id\";s:37:\"form-3ae73833f08accc7abe5517347ea87eb\";s:7:\"contact\";i:0;}",
"country_iso_code_2":"SA",
"timezone_name":"Asia/Riyadh",
"block":{
"webform":{
"client-block-88":1
}
},
"form_build_id":"form-3ae73833f08accc7abe5517347ea87eb",
"contact":0,
"roles":{
"2":"authenticated user"
}
}
}
how i can get the objects data or parse the result to get user name any help or examples will be appreciated 我如何获取对象数据或解析结果以获取用户名的任何帮助或示例将不胜感激
You need to use the NSJSONSerialization class method, JSONObjectWithData:options:error: to create an NSDictionary: 您需要使用NSJSONSerialization类方法JSONObjectWithData:options:error:创建一个NSDictionary:
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
if (! error) {
NSLog(@"%@",jsonDict);
}else{
NSLog(@"%@",error.localizedDescription);
}
This will get you to the point where you can look at the dictionary, which will be easier to read. 这将使您到达可以查看字典的位置,这将更易于阅读。 It looks like you need to use objectForKey:@"sessid" to get you to user, then objectForKey@"user", then objectForKey:@"name" to get you to the name.
看起来您需要使用objectForKey:@“ sessid”将您吸引到用户,然后使用objectForKey @“ user”,然后使用objectForKey:@“ name”将您吸引到该名称。
Check out this framework for parsing json. 查看此框架以解析json。 https://github.com/stig/json-framework/
https://github.com/stig/json-framework/
Also check out this answer iPhone/iOS JSON parsing tutorial . 还请查看此答案iPhone / iOS JSON解析教程 。 You'll find a link to a tutorial you can do to get acquainted with json parsing in ios.
您会找到指向教程的链接,您可以通过该链接熟悉iOS中的json解析。
See this answer and some code : 看到这个答案和一些代码:
NSMutableData *data; // Contains data received from the URL connection declares in header
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)dataIn
{
// Do it this way because connection doesn't guarantee all the data is in
POLLog(@" Tide View connection");
[data appendData:dataIn];
}
- (void) connectionDidFinishLoading:(NSURLConnection *) conn
{
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue]; // This is a new category added to the NSString by SBJSON
//100 parameters
for (int n=0;n<=100;n++)
{
// Get all the returned results
params[n] = [[results objectForKey:[NSString stringWithFormat:@"param%d",n]] floatValue];
}
To expand upon rdelmar's answer (which I think you should accept), you can use NSJSONSerialization
and then navigate the NSDictionary
results to extract the userName
: 要扩展rdelmar的答案 (我认为您应该接受),可以使用
NSJSONSerialization
,然后导航NSDictionary
结果以提取userName
:
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData
options:0
error:&error];
if (error == nil) {
NSDictionary *userDictionary = [jsonDict objectForKey:@"user"];
NSString *userName = [userDictionary objectForKey:@"name"];
// do what you need with the userName
} else {
NSLog(@"%@",error.localizedDescription);
}
Or if using the latest version of Xcode, you can replace those objectForKey
references with the even more concise Modern Objective-C syntax: 或者,如果使用最新版本的Xcode,则可以使用更简洁的Modern Objective-C语法替换那些
objectForKey
引用:
NSDictionary *userDictionary = jsonDict[@"user"];
NSString *userName = userDictionary[@"name"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.