gpt4 book ai didi

ios - 如何在 objective-c 中获取json数据的响应?

转载 作者:行者123 更新时间:2023-11-29 01:29:37 25 4
gpt4 key购买 nike

我用这段代码在 url 中发布我的数据。

NSString *urlString = [NSString stringWithFormat:@"company_name=%@&email_id=%@&password=%@",_CompanyName.text,_Email.text, _Password.text];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[urlString length]];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc]init];
NSString *url = @"http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=registration";
[req setURL:[NSURL URLWithString:url]];
[req setHTTPMethod:@"POST"];
[req setValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:[urlString dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:
^(NSURLResponse *reponse , NSData *data, NSError *error){
NSError *errre1;
dictLogin=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&errre1];
NSLog(@"result is%@ :",dictLogin);
NSLog(@"result is%@ :",urlString);
[user setObject:[dictLogin objectForKey:@"company_name"] forKey:@"company_name"];
[user setObject:[dictLogin objectForKey:@"email_id"] forKey:@"email_id"];
[user setObject:[dictLogin objectForKey:@"passord"] forKey:@"password"];

现在我不知道在登录屏幕时如何得到响应,

JSON

result : "Successful"
data
0
user_id : "1"
email_id : "gupta.monu4@gmail.com"
password : "monu"
phone_no : ""
user_type : "admin"
company_name : "Brainpulse"

这是我的 json 结构。

最佳答案

这样检查是为了避免崩溃和内存泄漏

  // create the one global array for save
NSMutableArray *user =[NSMutableArray new];

dictLogin=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&errre1];

if (dictLogin)
{
// serilize your data
if ([[dictLogin objectForKey:@"result"] isEqualToString:"Successful"])
{

[user removeAllobjects];
// assign into one array
NSArray *TempArray = dictLogin[@"data"];

for (NSDictionary *tmp in TempArray)
{
// careate one temporary dictionary
NSMutableDictionary *tmpDict = [NSMutableDictionary new];


// store your data to Dictionary or array
[tmpDict setObject:[tmp objectForKey:@"company_name"] forKey:@"company_name"];
[tmpDict setObject:[tmp objectForKey:@"email_id"] forKey:@"email_id"];

if ([tmp objectForKey:@"passord"] != nil) {
[tmpDict setObject:[tmp objectForKey:@"passord"] forKey:@"password"];
}


[user addobject:tmpDict];

// then use your data where u need

}
else
{
// show the failure report
}
}
else
{
// show the failure report
}

关于ios - 如何在 objective-c 中获取json数据的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33581456/

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