【问题标题】:Hapi - How to parse the payload to JSON?Hapi - 如何将有效负载解析为 JSON?
【发布时间】:2014-10-06 02:37:50
【问题描述】:

所以我正在尝试同时研究 HTTP/TCP/IP 协议、Nodejs、MongoJS 和 MongoDB。所以我试图用 Hapi Web 框架来解决一些问题。我可以使用我的 iOS 模拟器与服务器通信,但是我似乎无法正确解析有效负载。我知道我可以使用 URL 中的参数来发送信息,但我想使用有效负载。

所以我最终将它保存到 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 swift


    【解决方案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);

    【讨论】:

    • 好的,所以有效载荷的末尾有一个 ' = "" '。如果没有这部分,JSON 解析效果很好。你知道那个 = "" 部分是从哪里来的吗?
    • 你能粘贴新的有效载荷吗?
    • { '{"userLastNameKey":"Kwon6","userEmailKey":"email6","userFirstNameKey":"Michael6","userUsernameKey":"username6","userPasswordKey":"password6" }': '' }
    • 最后的“:''”让我很反感,你能解释一下那是什么吗?
    • 生成字符串的时候一定是出错了,能不能在你的帖子里加点代码让我看看?看起来您将整个有效负载作为键,并将 ''(empty) 作为值。
    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    相关资源
    最近更新 更多