如何在IOS6的JSON解析中获取和插入数据
我正在尝试使用 JSONParser 将数据插入 JSON 链接并从 JSON 链接检索数据。
如何在 IOS6 中不添加 JSON 框架的情况下使用 JSON Parser。
首先你需要导入 JSON 文件,你会从这里得到
然后,如果您使用的是 POST 方法,请执行以下操作:
NSString *myRequestString = [NSString stringWithFormat:@"u=%@&p= %@",strUname,strPassword];
myRequestString=[myRequestString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: @"Your Webservice URL"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:myRequestData];
NSData *returnData = nil;
returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
NSDictionary *myDic = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
如果您使用的是 GET 方法,请使用以下方法:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your WebService with Parameters in URL"]]];
NSDictionary *MyDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
希望这会有所帮助。
NSString *Post=[NSString stringWithFormat:@"email=%@&username=%@&password=%@",,Text_Email.text,Text_User.text,Text_Password.text];
NSData *PostData = [Post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *PostLengh=[NSString stringWithFormat:@"%d",[Post length]];
NSURL *Url=[NSURL URLWithString:[NSString stringWithFormat:@"%@usersignup.php",ServerPath]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:PostLengh forHTTPHeaderField:@"Content-Lenght"];
[request setHTTPBody:PostData];
NSData *ReturnData =[NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil];
NSString *Response = [[NSString alloc] initWithData:ReturnData encoding:NSUTF8StringEncoding];
Response = [Response stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];