![](/img/trans.png)
[英]Serializing a custom object into NSDictionary or NSArray to use NSJSONSerialization
[英]iOS NSJSONSerialization nesting NSDictionary as a key when serializing for POST request
我正在嘗試將一些數據發布到正在運行的RoR服務器上,但是在序列化后無法格式化我要發送的NSDictionary。
這是我的Objective C代碼:
- (void)registerUser:(NSString *)provider uid:(NSString *)uid
inviteCode:(NSString *)inviteCode userInfo:(NSDictionary *)userInfo
{
NSLog(@"Attempting to register user");
// Construct the data to post
NSDictionary *post = @{
@"provider": provider,
@"uid": uid,
@"invite_code": inviteCode,
@"user_info": userInfo
};
// TODO need to format better so it works with rails
NSData *data = [NSJSONSerialization dataWithJSONObject:post options:0 error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_registerUserURL]];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:_defaultConfig];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest: request
fromData: data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
}];
[task resume];
}
我希望它以以下格式將數據發布到我的RoR服務器:
{\\"user_info\\":{\\"name\\":\\"Matt Fornaciari\\",\\"email\\":\\"mattforni@gmail.com\\",\\"city\\":\\"San Francisco, California\\"},\\"provider\\":\\"facebook\\",\\"uid\\":\\"10202835053250675\\",\\"invite_code\\":\\"a\\"}
但是序列化過程似乎正在嵌套我的字典:
2014-12-09T04:20:14.366921+00:00 app[web.1]: Started POST "/users/register" for 67.160.200.20 at 2014-12-09 04:20:14 +0000
2014-12-09T04:20:14.439711+00:00 app[web.1]: Completed 400 Bad Request in 22ms (Views: 0.2ms | ActiveRecord: 0.0ms)
2014-12-09T04:20:14.417370+00:00 app[web.1]: Processing by UsersController#register as */*
2014-12-09T04:20:14.417422+00:00 app[web.1]: Parameters: {"{\"user_info\":{\"name\":\"Matt Fornaciari\",\"email\":\"mattforni@gmail.com\",\"city\":\"San Francisco, California\"},\"provider\":\"facebook\",\"uid\":\"10202835053250675\",\"invite_code\":\"a\"}"=>nil}
因此,我所有的數據都到達了,但是它嵌套在另一個Map中,作為一個指向nil值的鍵 ,使得Ruby無法讀取所有數據。 任何人都經歷過並有任何建議嗎?
因此,看起來NSJSONSerialization在將JSON發送到Rails應用程序中似乎不能很好地發揮作用。 我可以通過從字符串手動創建NSData
對象來解決此問題。 以下是我用來編碼參數的代碼。
// Construct the data to post
NSString *post = [NSString stringWithFormat:
@"provider=%@&uid=%@&invite_code=%@&user_info[name]=%@&user_info[email]=%@&user_info[city]=%@",
provider, uid, inviteCode, userInfo[@"name"], userInfo[@"email"], userInfo[@"city"]
];
NSData *data = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
// Create and send the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_registerUserURL]];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:_defaultConfig];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest: request
fromData: data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json);
}];
我遇到了同樣的問題,這是一個出奇的簡單解決方法。 您只需要在請求中將內容類型設置為“ application / json”,否則您的HTTP請求將無法正確解釋,並導致此奇怪的錯誤。
要解決此問題,您可以在原始帖子中調用setHTTPMethod之后添加以下行:
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.