JSON输出为空白值的键

所以我有一个字典,我试图从iOS设备发送到Node Hapi服务器。

由于某种原因,我的请求有效载荷是添加字典作为一个空白值的关键。 这是我的iOS代码

NSDictionary *userData = [User userToDictionary: newUserInfo]; NSData *userJSON = [NSJSONSerialization dataWithJSONObject: userData options: kNilOptions error: nil]; NSString *urlString = [NSString stringWithFormat: @"%@%@/addNewUser", kBaseURL, kUsers]; // This will set up the URL NSURL *url = [NSURL URLWithString: urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url]; [request setHTTPMethod: @"POST"]; [request addValue: @"application/json" forHTTPHeaderField: @"Content-Type"]; // This will set up the url session NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration: config]; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest: request fromData: userJSON completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if(error) { } else { } }]; [uploadTask resume]; 

这里是我的有效载荷

 { '{"userLastNameKey":"Kwon7","userEmailKey":"email7","userFirstNameKey":"Michael7","userUsernameKey":"username7","userPasswordKey":"password7"}': '' } 

谁能帮我这个? 我不想把我的用户信息保存在mongo中作为一个关键。 看起来很奇怪

即使OP已经解决了他的问题,并解释了什么是错的,作为答案发帖。 他说这是标题。 我有完全相同的问题,解决scheme是具体的Content-Type头。 由于我们将JSON对象传递给REST客户端,因此我们认为它设置了正确的Content-Type ,但事实并非如此。

 Content-Type: application/json 

HAPI 不会假定请求有效载荷是JSON。 JSON.stringify(request.payload, null, 2); 显示与问题中的问题相同的格式。 您必须为Hapi.js指定Content-Type才能将JSON有效负载正确parsing为JSON。