int格式的Json数据

时间:2012-09-15 12:44:55

标签: ios json nsstring nsinteger

我正在解析json,url返回一个整数值。 (例如278)

-(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];
}

提前致谢。

1 个答案:

答案 0 :(得分: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]

将其作为整数。