2014-10-06 52 views
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]; 
     } 

任何人都知道我能做到這一點?

回答

1
{ 
    "_id" = 5431f161bb859872034d2456; 
    "userLastNameKey" = "Kwon"; 
    "userEmailKey" = "email"; 
    "userFirstNameKey"= "Michael"; 
    "userUsernameKey" = "username"; 
    "userPasswordKey" = "password"; 
} 

也就是說不是一個JSON對象。您需要使用逗號而不是分號。和冒號而不是等號。你的ID也不是十進制數,所以最好把它作爲一個字符串。

你的對象將是這樣的:

{ 
    "_id" : "5431f161bb859872034d2456", 
    "userLastNameKey" : "Kwon", 
    "userEmailKey" : "email", 
    "userFirstNameKey" : "Michael", 
    "userUsernameKey" : "username", 
    "userPasswordKey" : "password" 
} 

爲了解析(格式良好的)JSON字符串,使用:var JSONObject = JSON.parse(JSONstring);

+0

確定這樣的有效載荷有一個「=‘’」在它的結束。如果沒有那部分,JSON解析就會很好。你知道可能在哪裏=「」部分來自? – 2014-10-06 04:02:55

+0

你可以粘貼新的有效載荷嗎? – xShirase 2014-10-06 04:26:56

+0

{'{「userLastNameKey」:「Kwon6」,「userEmailKey」:「email6」,「userFirstNameKey」:「Michael6」,「userUsernameKey」:「username6」,「userPasswordKey」:「password6」}':''} – 2014-10-06 04:28:13

相關問題