在iOS中创build一个JsonString

我是iOS新手。 我创build了一个像这样的JSON NSDictionary

 NSArray *keys = [NSArray arrayWithObjects:@"User", @"Password", nil]; NSArray *objects = [NSArray arrayWithObjects:@"Ali", @"2020", nil]; NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

然后我可以通过两种机制将其转换为NSString

1)

 NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error]; NSString *jsonString = nil; if (! jsonData) { NSLog(@"Got an error: %@", error); } else { jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; } 

2)

 NSString *jsonString = [jsonDictionary JSONRepresentation]; 

第二种方法,我得到这个warning

 Instance method '-JSONRepresentation' not found (return type defaults to 'id') 

但是,当我运行该项目,这两个机制工作正常:

 NSLog(@"Val of json parse obj is %@",jsonString); 

你知道我怎样才能以第二种方式删除警告?

我的主要目标是将此jsonstringPOST到使用RESTful Web Service的外部数据库。 考虑到我的主要目标,基本上哪个方法更好?

你应该使用NSJSONSerialization因为它更快,并直接与iOS SDK,只要你的“目标受众”是iOS5 +

要将数据发布到您的Web服务,您需要沿着这些线创build一个请求…

 NSDictionary * postDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"value1", @"value2", nil] forKeys:[NSArray arrayWithObjects:@"key1", @"key2", nil]]; NSError * error = nil; NSData * jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONReadingMutableContainers error:&error]; NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"your_webservice_post_url"]]; [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPBody:jsonData]; NSURLConnection * myConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES]; 

请阅读NSURLConnectionDelegate协议。

对于iOS 5.0>

像这样使用NSJSONSerialization :

 NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error]; NSString *resultAsString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"jsonData as string:\n%@ Error:%@", resultAsString,error); 

对于<iOS 5

使用json-framework第三方库,使用NSDictionary类别提供jsonstring:

  NSString *jsonString = [dictionary JSONRepresentation]; //with options NSString *jsonString = [dictionary JSONStringWithOptions:JKSerializeOptionNone error:nil] 

这将帮助你… 使用SBJson将NSDictionary转换为JSON

用这种方式,我希望它会帮助你

  NSArray *keys = [NSArray arrayWithObjects:@"User", @"Password", nil]; NSArray *objects = [NSArray arrayWithObjects:@"Ali", @"2020", nil]; NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; NSError *error = nil; // NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error]; id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; NSLog(@"\n\n\n id for json==%@ \n\n\n\n\n",result); 
 JSON DEFAULT METHOD...... +(NSDictionary *)stringWithUrl:(NSURL *)url postData:(NSData *)postData httpMethod:(NSString *)method { NSDictionary *returnResponse=[[NSDictionary alloc]init]; @try { NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:180]; [urlRequest setHTTPMethod:method]; if(postData != nil) { [urlRequest setHTTPBody:postData]; } [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [urlRequest setValue:@"text/html" forHTTPHeaderField:@"Accept"]; NSData *urlData; NSURLResponse *response; NSError *error; urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; returnResponse = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error]; } @catch (NSException *exception) { returnResponse=nil; } @finally { return returnResponse; } } Return Method : +(NSDictionary )methodName:(NSString)string { NSDictionary *returnResponse; NSData *postData = [NSData dataWithBytes:[string UTF8String] length:[string length]]; NSString *urlString = @"https//:..url...."; returnResponse=[self stringWithUrl:[NSURL URLWithString:urlString] postData:postData httpMethod:@"POST"]; return returnResponse; }