Reputation: 47
I parse an JSON File into an Dictionary and in a further step I want to build a post request for couchDB.
The parsing works fine, but if I post I get an error. I thinks it has something to do with my escape sequence in the post string.
Here´s the code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
// getting the data from file
NSString *_id = (NSString *) [data objectForKey:@"_id"];
NSString *rev_ = self.rev;
NSString *_herausgeber = (NSString *) [data objectForKey:@"Herausgeber"];
NSString *_nummer = (NSString *) [data objectForKey:@"Nummer"];
NSNumber *_deckung = [data objectForKey:@"Deckung"];
NSString *_waerhung = (NSString *) [data valueForKey:@"Waehrung"];
NSDictionary *_inhaber = (NSDictionary *) [data objectForKey:@"Inhaber"];
NSString *_name = (NSString *) [_inhaber objectForKey:@"Name"];
NSString *_vorname = (NSString *) [_inhaber objectForKey:@"Vorname"];
NSNumber *_maennlich = (NSNumber *) [_inhaber objectForKey:@"maennlich"];
NSArray *_hobbys = (NSArray *) [_inhaber objectForKey:@"Hobbys"];
NSString *_hobby0 = [_hobbys objectAtIndex:0];
NSString *_hobby1 = [_hobbys objectAtIndex:1];
NSString *_hobby2 = [_hobbys objectAtIndex:2];
NSNumber *_alter = (NSNumber *) [_inhaber objectForKey:@"Alter"];
NSArray * _kinder = (NSArray *) [_inhaber objectForKey:@"Kinder"];
NSString *_kind0 = [_kinder objectAtIndex:0];
NSString *_kind1 = [_kinder objectAtIndex:1];
NSString *_kind2 = [_kinder objectAtIndex:2];
NSString *_partner = (NSString *) [_inhaber objectForKey:@"Partner"];
[parser release];
//post string:
NSString *post = [NSString stringWithFormat:@"{\"_id\":\"%@\",\"_rev\":\"%@\",\"Herausgeber\":\"%@\",\"Nummer\":\"%@\",\"Deckung\":%@,\"Waehrung\":\"%@\",\"Inhaber\":{\"Name\":\"%@\",\"Vorname\":\"%@\",\"maennlich\":%@,\"Hobbys\":[\"%@\",\"%@\",\"%@\"],\"Alter\":%@,\"Kinder\":[\"%@\",\"%@\",\"%@\"],\"Partner\":%@}}",_id,rev_,_herausgeber,_nummer,_deckung,_waerhung,_name,_vorname,_maennlich,_hobby0,_hobby1,_hobby2,_alter,_kind0,_kind1,_kind2,_partner];
//post header:
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:cdbURL]];
[request setHTTPMethod:@"PUT"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
The Error I get:
Reply: {"error":"bad_request","reason":"invalid UTF-8 JSON: <<\"{\\"_id\\":\\"161eba7093799b610502bdfba5004281\\",\\"_rev\\":\\"199-bb065cbd0a365b188fc492cc22453d74\\",\\"Herausgeber\\":\\"SAP\\",\\"Nummer\\":\\"1234-5678-9012-3456\\",\\"Deckung\\":2000000,\\"Waehrung\\":\\"Franc\\",\\"Inhaber\\":{\\"Name\\":\\"Mustermann\\",\\"Vorname\\":\\"Max\\",\\"maennlich\\":1,\\"Hobbys\\":[\\"Reiten\\",\\"Golfen\\",\\"Lesen\\"],\\"Alter\\":42,\\"Kinder\\":[\\"Max\\",\\"Moritz\\",\\"Lisa\\"],\\"Partner\\":}}\">>"}
How can I solve this problem?
Upvotes: 1
Views: 412
Reputation: 4909
Personally, I would avoid constructing the JSON post-string myself and instead use SBJson's [NSObject JSONRepresentation] function.
Then you could fill an NSDictionary and have the API construct the JSON for you.
NSString *_id = @"161";
NSString *_rev = @"199";
NSString *_herausgeber = @"SAP";
NSMutableDictionary *toPost = [NSMutableDictionary dictionary];
[toPost setObject:_id forKey:@"_id"];
[toPost setObject:_rev forKey:@"_rev"];
[toPost setObject:_herausgeber forKey:@"Herausgeber"];
NSLog(@"JSON: %@", [toPost JSONRepresentation]);
Gives:
JSON: {"_id":"161","_rev":"199","Herausgeber":"SAP"}
Upvotes: 1