如何获得和iOS6的

问题描述:

插入JSON解析数据如何获得和iOS6的如何获得和iOS6的

我试图在以JSON链接,插入数据,并通过使用JSONParser检索JSON链路数据插入JSON解析数据。

如何在不添加JSON帧的情况下使用JSON解析器在IOS6中工作。

首先,你需要导入JSON文件,你会得到从here

然后,如果你正在使用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]; 

希望这会帮助。

你是什么意思的“没有添加JSON框架”?这是否意味着你想自己解析JSON?为什么你希望在存在好的图书馆时重新发明轮子?

我推荐JSONKit,一个快速写得很好的Objective-C库。只需将两个文件包含在项目中,即可解析和序列化JSON中的数据。

如果您希望学习JSON并希望为学习目的创建自己的库,请首先阅读this以了解JSON是什么以及它的语法是什么。从这里你可以思考如何实现一个字典到JSON(甚至是对象到JSON)。

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]]; 
+0

欢迎使用StackOverflow。你应该评论你的代码并解释步骤,而不是仅仅粘贴一堆代码。谢谢! – 2013-08-16 10:03:34