【发布时间】: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
【问题讨论】: