如何在IOS6的JSON解析中获取和插入数据

如何在IOS6的JSON解析中获取和插入数据,json,ios6,iphone-5,Json,Ios6,Iphone 5,如何在IOS6的JSON解析中获取和插入数据 我试图将数据插入JSON链接,并使用JSONParser从JSON链接检索数据 我如何使用JSON解析器而不在IOS6中添加JSON框架。您所说的“不添加JSON框架”是什么意思?这是否意味着您希望自己解析JSON?当存在好的库时,你为什么希望重新发明轮子 我推荐一个快速且编写良好的Objective-C库。只需在项目中包含两个文件,就可以将数据从JSON解析并序列化为JSON 如果您希望学习JSON并希望创建自己的库以供学习,请先阅读以了解什么是J

如何在IOS6的JSON解析中获取和插入数据

我试图将数据插入JSON链接,并使用JSONParser从JSON链接检索数据

我如何使用JSON解析器而不在IOS6中添加JSON框架。

您所说的“不添加JSON框架”是什么意思?这是否意味着您希望自己解析JSON?当存在好的库时,你为什么希望重新发明轮子

我推荐一个快速且编写良好的Objective-C库。只需在项目中包含两个文件,就可以将数据从JSON解析并序列化为JSON

如果您希望学习JSON并希望创建自己的库以供学习,请先阅读以了解什么是JSON及其语法。从这里,您可以考虑如何实现JSON字典(甚至JSON对象)。

您所说的“不添加JSON框架”是什么意思?这是否意味着您希望自己解析JSON?当存在好的库时,你为什么希望重新发明轮子

我推荐一个快速且编写良好的Objective-C库。只需在项目中包含两个文件,就可以将数据从JSON解析并序列化为JSON


如果您希望学习JSON并希望创建自己的库以供学习,请先阅读以了解什么是JSON及其语法。从这里,您可以考虑如何实现JSON字典(甚至是JSON对象)。

首先,您需要导入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];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your WebService with Parameters in URL"]]];
NSDictionary *MyDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
如果您使用的是GET方法,请使用以下方法:

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];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your WebService with Parameters in URL"]]];
NSDictionary *MyDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

希望这会有所帮助。

首先,您需要导入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]];
然后,如果使用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];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your WebService with Parameters in URL"]]];
NSDictionary *MyDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
如果您使用的是GET方法,请使用以下方法:

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];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"Your WebService with Parameters in URL"]]];
NSDictionary *MyDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

希望这会有所帮助。

欢迎使用StackOverflow。您应该注释代码并解释步骤,而不是仅仅粘贴一堆代码。谢谢欢迎收看StackOverflow。您应该注释代码并解释步骤,而不是仅仅粘贴一堆代码。谢谢
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]];