gpt4 book ai didi

http请求后ios解析json结果

转载 作者:行者123 更新时间:2023-11-28 20:27:59 25 4
gpt4 key购买 nike

我开始构建通过 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);
}

我得到这个结果:

{"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",
"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"
}
}
}

我如何获取对象数据或解析结果以获取用户名任何帮助或示例将不胜感激

最佳答案

您需要使用 NSJSONSerialization 类方法 JSONObjectWithData:options:error: 创建一个 NSDictionary:

NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
if (! error) {
NSLog(@"%@",jsonDict);
}else{
NSLog(@"%@",error.localizedDescription);
}

这将使您可以查看字典,这将更容易阅读。看起来您需要使用 objectForKey:@"sessid"来获取用户,然后使用 objectForKey@"user",然后使用 objectForKey:@"name"来获取名称。

关于http请求后ios解析json结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13335948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com