0

我正在解析 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];
}

提前致谢。

4

1 回答 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]

将其作为整数。

于 2012-09-15T12:47:19.293 回答