【问题标题】:Convert Binance String To object for reaching each member of object将 Binance String 转换为对象以到达对象的每个成员
【发布时间】:2023-03-06 10:57:01
【问题描述】:

在 Nodejs 中,我有一个 Binace 返回的字符串,如下所示:

内容类型:application/json;charset=UTF-8\r\n内容长度:312\r\n连接:关闭\r\n日期:2021 年 5 月 12 日星期三 08:02:40 GMT\r\n服务器: nginx\r\nvary: Accept-Encoding\r\nx-mbx-uuid: b2bee016-a506-46cc-9bb1-1d04eee4666f\r\nx-mbx-used-weight: 81\r\nx-mbx-used-weight -1m: 81\r\nx-mbx-order-count-10s: 1\r\nx-mbx-order-count-1d: 6\r\nstrict-transport-security: max-age=31536000; includeSubdomains\r\nx-frame-options: SAMEORIGIN\r\nx-xss-protection: 1; mode=block\r\nx-content-type-options: nosniff\r\ncontent-security-policy: default-src 'self'\r\nx-content-security-policy: default-src 'self'\r\ nx-webkit-csp: default-src 'self'\r\ncache-control: no-cache, no-store, must-revalidate\r\npragma: no-cache\r\nexpires: 0\r\naccess-control -allow-origin: *\r\naccess-control-allow-methods: GET, HEAD, OPTIONS\r\nx-cache: Miss from cloudfront\r\nvia: 1.1 f7807c0a57cfa18eb5f00429067b5f6a.cloudfront.net (CloudFront)\r\nx -amz-cf-pop: SYD1-C1\r\nx-amz-cf-id: gbBnOGuEFsR7_bOthF2qRpieQ-pimX133hz7z76fEaTK7xR3KWlDGg==

你可以看到这个字符串包含:和\n\r。 我的问题是如何将此字符串转换为这样的对象:

viResponse = {
    content-type: "application/json;charset=UTF-8",
    content-length: "312",
    connection: "close",
    date: "Wed, 12 May 2021 08:02:40 GMT",
    server: "nginx",
    vary: "Accept-Encoding",
    x-mbx-uuid: "b2bee016-a506-46cc-9bb1-1d04eee4666f",
    x-mbx-used-weight: "81",
    x-mbx-used-weight-1m: "81",
    x-mbx-order-count-10s: "1",
    x-mbx-order-count-1d: "6",
    strict-transport-security: "max-age=31536000; includeSubdomains",
    x-frame-options: "SAMEORIGIN",
    x-xss-protection: "1; mode=block",
    x-content-type-options: "nosniff",
    content-security-policy: "default-src 'self'",
    x-content-security-policy: "default-src 'self'",
    x-webkit-csp: "default-src 'self'",
    cache-control: "no-cache", no-store", must-revalidate",
    pragma: "no-cache",
    expires: "0",
    access-control-allow-origin: "*",
    access-control-allow-methods: "GET", HEAD", OPTIONS",
    x-cache: "Miss from cloudfront",
    via: "1.1 f7807c0a57cfa18eb5f00429067b5f6a.cloudfront.net (CloudFront)",
    x-amz-cf-pop: "SYD1-C1",
    x-amz-cf-id: "gbBnOGuEFsR7_bOthF2qRpieQ-pimX133hz7z76fEaTK7xR3KWlDGg==",
}

请注意,我想将提到的字符串转换为能够以每个参数作为其键的对象。

console.log(viResponse['x-mbx-uuid']);   // b2bee016-a506-46cc-9bb1-1d04eee4666f

【问题讨论】:

    标签: node.js string object


    【解决方案1】:

    可能的解决方案可能如下所示。虽然每个值都是一个字符串,所以如果你需要接收一个数字,你需要手动转换它。

    function responseToObject(response) {
        const result = {};
        const fields = response.split("\r\n");
        for (const field of fields) {
            const [name, ...value] = field.split(":");
    
            result[name] = value.join(":").trim();
        }
    
        return result
    }
    

    根据评论添加了reduce方法的替代实现。

     function responseToObject(response) {
        const fields = response.split("\r\n");
    
        return fields.reduce((result, field) => {
            const [name, ...value] = field.split(":");
    
            result[name] = value.join(":").trim();
            return result;
        }, {});
    }
    

    结果是一样的。如果你愿意,你可以缩短它。

     function responseToObject(response) {   
        return response.split("\r\n").reduce((result, field) => {
            const [name, ...value] = field.split(":");
    
            result[name] = value.join(":").trim();
    
            return result;
        }, {});
    }
    

    例如对于你的字符串,它返回

    【讨论】:

    • 您好,谢谢,它确实有效。但我认为映射方法有一种更简单、更快捷的方法。任何人都可以通过映射来做到这一点?
    • @Parsika 我已经更新了答案,包括使用reduce 方法的替代解决方案。这是最合适的一种。 map 方法无法做到这一点,因为它会返回一个数组作为结果。说到速度,很难说哪个变种更快。
    • 我会说第一个变量会更快,因为它使用原始的for 循环,因此我们的回调函数调用较少,但需要测试才能确定。
    • 感谢您的更新。您的新函数返回错误 ==>> TypeError: 无法设置未定义的属性“内容长度”。
    • 另外我必须提到你的函数不会处理这些值。以空格开头的值。为什么?
    猜你喜欢
    • 2011-11-03
    • 2021-11-19
    • 2010-12-12
    • 2011-02-07
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2012-04-04
    相关资源
    最近更新 更多