Question

i try to convert my object to a json string, without umlauts its working fine but when i type a text with "ä" or "ü" im getting this error:

ExceptionType = "System.ArgumentException";
Message = "Ein ung\U00fcltiges Objekt wurde \U00fcbergeben. Erwartet wurde \":\" oder \"}\". 

this is my code:

-(Buchungsergebnis*)sendeBuchung:(NSString *)Buchung vomWebservice:(NSString *)WebServiceURL
{
//Erstellt einen Request

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:WebServiceURL]];

[request setHTTPMethod: @"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSData *reqData = [NSData dataWithBytes:[Buchung UTF8String] length:[Buchung length]];
[request setHTTPBody:reqData];

NSURLResponse *response =[[NSURLResponse alloc]init];
NSError *error;
//Führt den Befehl aus
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data ==nil)
{
    NSLog(@"Login Error: %@",error);
    return false;
}
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init ];
dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"%@",dic);

NSMutableDictionary *result = [[NSMutableDictionary alloc]init];
result = [dic objectForKey:@"d"] ;

Buchungsergebnis *ergebnis = [[Buchungsergebnis alloc]init];
ergebnis.result =[[result objectForKey:@"result"]boolValue];
ergebnis.message =[result objectForKey:@"message"];


return ergebnis;
}

and here is the json:

{"thema":"Test with umlauts \n\nÖffne die tür","personenanzahl":"4","vondatum":"17.12.2013 09:00","veranstalteremail":"asjgdl@dgs.de","bisdatum":"17.12.2013 10:30","dienstleistungen":[],"teilnehmer":[],"veranstaltertelefon":"","veranstalter":"blagojevic domenik","bestuhlungsid":"377","benutzername":"dbl"}

how can i prevent this error?

Was it helpful?

Solution

Your JSON is valid (as can be tested on http://jsonlint.com).

But this is problematic:

NSData *reqData = [NSData dataWithBytes:[Buchung UTF8String] length:[Buchung length]];

because [Buchung length] returns the number of (Unicode) characters in the string, which is different from the number of UTF-8 bytes for non-ASCII characters such as umlauts. Better use

NSData *reqData = [Buchung dataUsingEncoding:NSUTF8StringEncoding];

Or use NSJSONSerialization to create the JSON, which gives you an NSData object instead of a string.

You might also have to set the HTTP Content-length explicitly, I am not sure if that is necessary:

[request setValue:[NSString stringWithFormat:@"%d", [reqData length]] forHTTPHeaderField:@"Content-Length"];

OTHER TIPS

try using: [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; (use NSJSONReadingMutableLeaves instead of kNilOptions)

I'm using this with data containing Chinese, Spanish, Hindi languages. It's working fine for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top