2
所以我試圖一次研究HTTP/TCP/IP協議,Nodejs,MongoJS和MongoDB。所以我試圖找出Hapi Web框架的一些東西。我能夠用我的iOS模擬器與服務器通信,但是,我似乎無法解析有效負載。我知道我可以使用URL中的參數發送信息,但我想使用有效載荷。Hapi - 如何解析有效載荷爲JSON?
所以我最終將這個保存到Mongo中。
(
{
"_id" = 5431f161bb859872034d2456;
"{\"userLastNameKey\":\"Kwon\",\"userEmailKey\":\"email\",\"userFirstNameKey\":\"Michael\",\"userUsernameKey\":\"username\",\"userPasswordKey\":\"password\"}" = "";
},
{
"_id" = 5431fe5694ed4721046c1f8c;
"{\"userLastNameKey\":\"Kwon2\",\"userEmailKey\":\"email2\",\"userFirstNameKey\":\"Michael2\",\"userUsernameKey\":\"username2\",\"userPasswordKey\":\"password2\"}" = "";
這是我的呼叫代碼。
// This will add a new user to the database
function addUser(request, response){
db.usersCollection.save(request.payload, function (err, saved){
if(err || !saved)
{
console.log("User not saved");
} else
{
console.log("User saved");
}
});
}
我的最終結果,我想嘗試,並得到它是這樣
(
{
"_id" = 5431f161bb859872034d2456,
"userLastNameKey" = "Kwon",
"userEmailKey" = "email",
"userFirstNameKey"= "Michael",
"userUsernameKey" = "username",
"userPasswordKey" = "password",
}
)
這裏是iOS的代碼
NSDictionary *userData = [User userToDictionary: newUserInfo];
NSData *userJSON = [NSJSONSerialization dataWithJSONObject: userData options: 0 error: nil];
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];
}
任何人都知道我能做到這一點?
確定這樣的有效載荷有一個「=‘’」在它的結束。如果沒有那部分,JSON解析就會很好。你知道可能在哪裏=「」部分來自? – 2014-10-06 04:02:55
你可以粘貼新的有效載荷嗎? – xShirase 2014-10-06 04:26:56
{'{「userLastNameKey」:「Kwon6」,「userEmailKey」:「email6」,「userFirstNameKey」:「Michael6」,「userUsernameKey」:「username6」,「userPasswordKey」:「password6」}':''} – 2014-10-06 04:28:13