我将JSON文件解析为字典,并且在另一步骤中我想为couchDB构建一个post请求。
解析工作正常,但如果我发布我得到一个错误。我认为它与post字符串中的转义序列有关。
这是代码:
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];
我得到的错误:
回复:{“error”:“bad_request”,“reason”:“无效的UTF-8 JSON: << \ “{\\” _ ID \\ “:\\” 161eba7093799b610502bdfba5004281 \\”,\\ “_转\\”:\\ “199-bb065cbd0a365b188fc492cc22453d74 \\”,\\ “Herausgeber \\”:\ \ “SAP \\”,\\ “Nummer \\”:\\ “1234-5678-9012-3456 \\”,\\ “Deckung \\”:2000000,以\\ “Waehrung \\”:\\”丽珠\\ “\\ ”INHABER \\“:{\\ ”名称\\“:\\ ”Mustermann \\“,\\ ”Vorname \\“:\\ ”最大\\“,\\” maennlich \\ “:1,\\” Hobbys \\ “:[\\” Reiten \\”,\\ “Golfen \\”,\\ “Lesen \\”],\\ “ALTER \\”:42, \\ “金德\\”:[\\ “最大\\”,\\ “莫里茨\\”,\\ “莉莎\\”],\\ “伙伴\\”:}} \“>> “}
我该如何解决这个问题?
答案 0 :(得分:1)
就个人而言,我会避免自己构建JSON后期字符串,而是使用SBJson的[NSObject JSONRepresentation]函数。
然后你可以填写NSDictionary并让API为你构建JSON。
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]);
给出:
JSON: {"_id":"161","_rev":"199","Herausgeber":"SAP"}