2012-08-03 58 views
1

我將JSON文件解析爲字典,並且在下一步中我想構建針對couchDB的發佈請求。將JSON文件解析爲POST請求目標C

解析工作正常,但如果我發佈我得到一個錯誤。我認爲它與我的轉義字符串中的轉義序列有關。

Here's代碼:

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]; 

的錯誤,我得到:

回覆:{ 「錯誤」: 「BAD_REQUEST」, 「原因」:「無效的UTF-8 JSON: < < \「{\\ __ id \\」:\\「161eba7093799b610502bdfba5004281 \\」,\\「_ rev \\」:\\「199-bb065cbd0a365b188fc492cc22453d74 \\」,\\「Herausgeber \\」:\\ 「SAP \\」,\\ 「Nummer \\」:\\ 「1234-5678-9012-3456 \\」,\\ 「Deckung \\」:2000000,以\\ 「Waehrung \\」:\\「法郎\\ 「\\ 」INHABER \\「:{\\ 」名稱\\「:\\ 」Mustermann \\「,\\ 」Vorname \\「:\\ 」最大\\「,\\」 maennlich \ \「:1 ,\\ 「Hobbys \\」:[\\ 「Reiten \\」,\\ 「Golfen \\」,\\ 「Lesen \\」],\\ 「ALTER \\」:42,\\「金德\ \「:\\」Max \\「,\\」Moritz \\「,\\」Lisa \\「],\\」Partner \\「:}} \」>>「}

我該如何解決這個問題?

回答

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"} 
+0

確定。感謝提示。由於我是Objective C的新手,你能否給我一個代碼示例如何做到這一點? – user1573643 2012-08-03 10:01:01

+0

如果您使用的是iOS 5.0(或更高版本),則可以使用NSJSONSerialization類http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html – Francesco 2012-08-03 10:06:09

+0

謝謝,幫助過我! – user1573643 2012-08-03 10:23:18