从JSON中删除多余的空格和新行

时间:2016-10-18 08:12:38

标签: ios json xcode

下面是我的代码,我正在尝试解析JSON;我得到了回复,但是当我打印字典时,它是空的。

下面是响应字符串,JSON的结果。

 NSString *post = [[NSString alloc] initWithFormat:@"jobid=%@",idjob];
 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
 NSURL *url = [NSURL URLWithString:@"URL"];
 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
 [theRequest setHTTPMethod:@"POST"];
 [theRequest setHTTPBody:postData];
 NSURLResponse *response;
 NSError *error;
 NSData *urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
 NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
 NSLog(@"login:%@",str);

 NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];

 NSLog(@"aarraa:%@",jsonDict);

对str的回应:

  {"Jobdesc":[{"jobid":"11260","job_title":"Linux + Nagios System Administrator","job_desc":"<p>Technical skills required:</p>

   <ol>
  <li>Should be ready to work during French timings</li>
  <li>Linux Certification / training is a must</li>
  <li>Linux System administration (Red hat Linux, CentOS Servers)</li>
  <li>Experience in LAMP configuration and troubleshooting</li>
  <li>Knowledge on windows OS</li>
  <li>Experience on monitoring tools like Nagios / Centreon,    Ops5</li>
  <li>Scripting in Shell, Perl or Python</li>
 </ol>
","job_role":"Linux + Nagios System Administrator","job_exp":"1-5 year","job_education":"Others","job_location":"Delhi","job_address":"Delhi","job_company_name":"Pandya Business Solutions.","job_company_url":"http://www.pandyabusinesssolutions.com/","job_company_email":"singhjapesh@gmail.com","job_status":""}]}

但参数jsonDict为null。

4 个答案:

答案 0 :(得分:0)

像这样序列化数据。

NSMutableDictionary *json =[NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];

答案 1 :(得分:0)

试试这个......

NSString *jsonString = [NSString stringWithFormat:@"URL"];

NSURL *nurl = [NSURL URLWithString:jsonString];
NSData *jsonData = [NSData dataWithContentsOfURL:nurl];
NSError *error = nil;


NSDictionary *dictResult = [NSJSONSerialization
                                JSONObjectWithData:jsonData options:0 error:&error];

答案 2 :(得分:0)

您似乎正在尝试在完全获取之前序列化您的数据,下面是带有completionHandler的代码,一旦您获得所有数据,它将开始进一步处理,所以试试这个:

[NSURLConnection sendAsynchronousRequest: theRequest queue [NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

            NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
    }];

答案 3 :(得分:0)

我尝试验证JSON响应,发现JSON中有额外的空格,所以下面是我已解决的答案。

  NSString *post = [[NSString alloc] initWithFormat:@"jobid=%@",idjob];
  NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  NSURL *url = [NSURL URLWithString:@"http://ncrjobs.in/webservice/jobdesc.php"];
  NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
 [theRequest setHTTPMethod:@"POST"];
 [theRequest setHTTPBody:postData];
 NSURLResponse *response;
 NSError *error;
 NSData *urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
 NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
 NSCharacterSet *spaces=[NSCharacterSet whitespaceCharacterSet];
 NSPredicate *predicates=[NSPredicate predicateWithFormat:@"SELF !=''"];
 NSArray *temparray=[[str componentsSeparatedByCharactersInSet:spaces]filteredArrayUsingPredicate:predicates];
 str=[temparray componentsJoinedByString:@" "];
 NSString  *string = [str stringByReplacingOccurrencesOfString:@"[\r\n]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, str.length)];

 NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];