0

我正在尝试获取一个 JSON 字符串,但有些单词有“á、é、ç”等。

JSON是:

{"Error":"", "Result":{"Transacoes": [{"ClienteID":"1","ID":"915","Banco":"Bradesco","Fornecedor":"","Tipo":"Cartão de crédito - Bradesco Visa","ValorCredito":"0,0000","ValorDebito":"4000,0000","Vencimento":"","Pagamento":"03/08/2012 00:00:00","Descricao":"Cartão Bradesco Visa","Lançamento":"03/08/2012 15:18:12"},{"ClienteID":"1","ID":"916","Banco":"Bradesco","Fornecedor":"","Tipo":"Alinhamento da Conta Bancária","ValorCredito":"22398,9200","ValorDebito":"0,0000","Vencimento":"","Pagamento":"02/08/2012 00:00:00","Descricao":"FGTS","Lançamento":"07/08/2012 11:12:16"}]}}

代码是:

-(void)viewWillAppear:(BOOL)animated {
NSString *urlAddress = [NSString stringWithFormat:@"http://SITE?action=transacoes&AuthToken=%@&DataDe=02/08/2012&DataAte=03/08/2012", self.usuario.authToken];

NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request
                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"%@", JSON);
                                        }
                                        failure: ^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON){
                                            NSLog(@"ERROR: %@", error);
                                        }];

[operation start];
}
4

1 回答 1

0

您的应用程序抛出Exception,因为它无法从您请求的服务器获取数据。所以首先确保它接收到一些数据,然后实现以下代码

尝试使用以下代码来解析您的 json 数据,我尝试使用我的应用程序,它也可以与特殊符号一起正常工作。

NSString *urlAddress = [NSString stringWithFormat:@"http://SITE?action=transacoes&AuthToken=%@&DataDe=02/08/2012&DataAte=03/08/2012", self.usuario.authToken];

NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSHTTPURLResponse* urlResponse = nil;

NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

if([responseData bytes] > 0)
{
    // It will work only IOS 5.0 or later version otherwise you can use any third party JSON parsers (eg. SBJSON)
    NSDictionary* json = [NSJSONSerialization
                          JSONObjectWithData:responseData
                          options:NSJSONReadingMutableLeaves
                          error:&error];
    NSLog(@"%@",[[[[json objectForKey:@"Result"] objectForKey:@"Transacoes"] objectAtIndex:1] valueForKey:@"Descricao"]);
    NSLog(@"%@",[[[[json objectForKey:@"Result"] objectForKey:@"Transacoes"] objectAtIndex:1] valueForKey:@"Tipo"]);
}
于 2013-04-23T11:16:14.500 回答