1

我将一个 JSON 文件解析为一个字典,然后我想为 couchDB 构建一个发布请求。

解析工作正常,但如果我发布我得到一个错误。我认为这与我在帖子字符串中的转义序列有关。

这是代码:

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","re​​ason":"无效的 UTF-8 JSON:<<\"{\\"_id\\":\\"161eba7093799b610502bdfba5004281\\",\\"_rev\\" :\\"199-bb065cbd0a365b188fc492cc22453d74\\",\\"Herausgeber\\":\\"SAP\\",\\"数字\\":\\"1234-5678-9012-3456\\",\ \"Deckung\\":2000000,\\"Waehrung\\":\\"法郎\\",\\"Inhaber\\":{\\"姓名\\":\\"Mustermann\\", \\"Vorname\\":\\"Max\\",\\"maennlich\\":1,\\"爱好\\":[\\"Reiten\\",\\"Golfen\\" ,\\"Lesen\\"],\\"Alter\\":42,\\"Kinder\\":[\\"Max\\",\\"Moritz\\",\\"丽莎\ \"],\\"合作伙伴\\":}}\">>"}

我怎么解决这个问题?

4

1 回答 1

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"}
于 2012-08-03T09:43:14.990 回答