2012-09-15 60 views
0

我正在解析json并且url正在返回一个整数值。 (例如278)JSON int格式的数据

-(void) connectionDidFinishLoading: (NSURLConnection *) connection{ 
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
NSString *responseString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSLog(@"JSON Response = %@",responseString);} 
当我在打印响应的NSLog

,它给出了这样的事情

2012-09-15 18:02:02.091 Web Service[5190:f803] JSON Response = "278"

我不想在引号输出。我希望它喜欢

2012-09-15 18:02:02.091 Web Service[5190:f803] JSON Response = 278

我怎样才能做到这一点?

JSON提前

NSString *urlString = [NSString stringWithFormat:@"http://10.5.6.105/ARAPPWS/Service1/InsertEmp/name=%@,phone=%@",name.text,number.text]; 
    NSURL *addUrl = [NSURL URLWithString:urlString]; 
    NSURLRequest *addRequest = [NSURLRequest requestWithURL:addUrl]; 
    (void)[NSURLConnection connectionWithRequest:addRequest delegate:self]; 

-(void) connection: (NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response 
{ 
jsonData = [[NSMutableData alloc]init]; 


} 
-(void) connection: (NSURLConnection *) connection didReceiveData:(NSData *)data 
{ 
[jsonData appendData:data]; 

} 

-(void) connectionDidFinishLoading: (NSURLConnection *) connection 
{ 
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
responseString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

[self performSelector:@selector(uploadEmpImg:) withObject:nil afterDelay:1]; 
} 

感谢。

回答

0

您可以使用- [NSString integerValue]获得字符串的实际数值表示:

NSLog(@"JSON response = %d", [responseString integerValue]); 

编辑:问题是,它返回一个JSON片段 - 严格来说,它不是有效的JSON。然后,您可以使用

[responseString substringWithRange:NSMakeRange(1, responseString.length - 2)] 

获得不带引号的实际字符串值和

[[responseString substringWithRange:NSMakeRange(1, responseString.length - 2)] intValue] 

把它作为一个整数。

+0

它给我JSON响应= 0而不是JSON响应= 278 –

+0

@Ankur所以web服务返回'278'或'“278”'?顺便说一下,为什么不使用适当的JSON解析器而不是黑客? – 2012-09-15 12:59:51

+0

我的web服务返回278. 我是新来的JSON解析我不知道如何使用正确的JSON。 –