gpt4 book ai didi

javascript - Node JS,如何在 JSON 字符串中正确存储 Base64 字符串?

转载 作者:搜寻专家 更新时间:2023-10-31 23:59:30 30 4
gpt4 key购买 nike

我必须生成带有一些属性的 JSON,其中之一是 base64 字符串:

var file = fs.readFileSync('myfile');
var request = { password: '123456', key: file.toString('base64')};
console.log(JSON.stringify(request));

它向我输出那种 JSON:

{"password":"123456","key":"MIIIhQIBAzCCCD8GCSqGSIb3DQEHAaCCCDAEgggsMIAwgAYJKoZIhvcNA
QcBoIAEggEOMIIBCjCCAQYGCyqGSIb3DQEMCgECoG4wbDAoBgoqhkiG9w0BDAEDMBoEFOWB9Rf4cT/NBObHUQW6sCOtB2AbAgIEAARAfTkFJwD66tZ2p30iXGLNtyH41D4RlWSof/MJ3w91rU22sQmCZkRR7UzgOlaoFWggWB2a2FHYOMpNjRbm+d
9+hjGBhjAjBgkqhkiG9w0BCRUxFgQU5W/loImfeHgVra9dJW2noKcMLBMwXwYJKoZIhvcNAQkUMVIeUABlADUANgBmAGUANQBhADAAOAA5ADkAZgA3ADgANwA4ADEANQBhAGQAYQBmADUAZAAyADUANgBkAGEANwBhADAAYQA3ADAAYwAyAGMAMQA
zAAAAADCABgkqhkiG9w0BBwaggDCAAgEAMIAGCSqGSIb3DQEHATAoBgoqhkiG9w0BDAEGMBoEFPOj+9YztiaevvtCfZthUx6+uv27AgIEAKCABIIGqHs+siCvaxABhn/GgVGEfZ/SYJN/wBszCjJDpyiv1dO1c3Ty3yPzxzqCwpvYvZcXLpzc6Xa6
v90tmrml63zIOwPyDSKqZ0nSC8fL+jqMRXArUYlDoP+TK39OknfPEhhjLmJOqY2jboRvsDXaOggu0g7VOhx4vG6e/nj8LjFgEQEzoqF5wxg/RTx5yNvnK3oUPocbKUjSgJ1qj26vJUk3vXNTGjks9ltswRZA5up+b+3bzx5Cy/gCU/U96bw3u+8Ei
p0NdtEsXzx5SmD70E85HPyPg7NAk7qrliy16MdJHKzN9gSDl6UDtLKNFPXvDPAjAkSPwKt1qp0SEn+aZ+BADIk2TYZDTRFxGD6ym9+mwBzSsBoJ5kojmyF3F2CJCQzjyEs2moCCV32sgHJ59k3C1DOGD4b2TNxRKm4U0hjpdXqjv2orPDpkvniejJ
"}

我刚刚跳过了这个问题中 base64 字符串的最后部分。

它为我生成了 JSON 字符串,但它是无效的,因为 base64 字符串的结构中有一些双反斜杠。

如何正确生成带有base64属性的JSON字符串?

我生成的 base64 看起来像这样:

MIIIhQIBAzCCCD8GCSqGSIb3DQEHAaCCCDAEgggsMIAwgAYJKoZIhvcNA
QcBoIAEggEOMIIBCjCCAQYGCyqGSIb3DQEMCgECoG4wbDAoBgoqhkiG9w0BDAEDMBoEFOWB9Rf4cT/NBObHUQW6sCOtB2AbAgIEAARAfTkFJwD66tZ2p30iXGLNtyH41D4RlWSof/MJ3w91rU22sQmCZkRR7UzgOlaoFWggWB2a2FHYOMpNjRbm+d
9+hjGBhjAjBgkqhkiG9w0BCRUxFgQU5W/loImfeHgVra9dJW2noKcMLBMwXwYJKoZIhvcNAQkUMVIeUABlADUANgBmAGUANQBhADAAOAA5ADkAZgA3ADgANwA4ADEANQBhAGQAYQBmADUAZAAyADUANgBkAGEANwBhADAAYQA3ADAAYwAyAGMAMQA
zAAAAADCABgkqhkiG9w0BBwaggDCAAgEAMIAGCSqGSIb3DQEHATAoBgoqhkiG9w0BDAEGMBoEFPOj+9YztiaevvtCfZthUx6+uv27AgIEAKCABIIGqHs+siCvaxABhn/GgVGEfZ/SYJN/wBszCjJDpyiv1dO1c3Ty3yPzxzqCwpvYvZcXLpzc6Xa6
v90tmrml63zIOwPyDSKqZ0nSC8fL+jqMRXArUYlDoP+TK39OknfPEhhjLmJOqY2jboRvsDXaOggu0g7VOhx4vG6e/nj8LjFgEQEzoqF5wxg/RTx5yNvnK3oUPocbKUjSgJ1qj26vJUk3vXNTGjks9ltswRZA5up+b+3bzx5Cy/gCU/U96bw3u+8Ei
p0NdtEsXzx5SmD70E85HPyPg7NAk7qrliy16MdJHKz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中不能有反斜杠,其余的都会转义。

Base64 index Table :

0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

对于给定的字符串,它完美地工作:

a={"password":"123456",key:"MIIIhQIBAzCCCD8GCSqGSIb3DQEHAaCCCDAEgggsMIAwgAYJKoZIhvcNAQcBoIAEggEOMIIBCjCCAQYGCyqGSIb3DQEMCgECoG4wbDAoBgoqhkiG9w0BDAEDMBoEFOWB9Rf4cT/NBObHUQW6sCOtB2AbAgIEAARAfTkFJwD66tZ2p30iXGLNtyH41D4RlWSof/MJ3w91rU22sQmCZkRR7UzgOlaoFWggWB2a2FHYOMpNjRbm+d9+hjGBhjAjBgkqhkiG9w0BCRUxFgQU5W/loImfeHgVra9dJW2noKcMLBMwXwYJKoZIhvcNAQkUMVIeUABlADUANgBmAGUANQBhADAAOAA5ADkAZgA3ADgANwA4ADEANQBhAGQAYQBmADUAZAAyADUANgBkAGEANwBhADAAYQA3ADAAYwAyAGMAMQAzAAAAADCABgkqhkiG9w0BBwaggDCAAgEAMIAGCSqGSIb3DQEHATAoBgoqhkiG9w0BDAEGMBoEFPOj+9YztiaevvtCfZthUx6+uv27AgIEAKCABIIGqHs+siCvaxABhn/GgVGEfZ/SYJN/wBszCjJDpyiv1dO1c3Ty3yPzxzqCwpvYvZcXLpzc6Xa6v90tmrml63zIOwPyDSKqZ0nSC8fL+jqMRXArUYlDoP+TK39OknfPEhhjLmJOqY2jboRvsDXaOggu0g7VOhx4vG6e/nj8LjFgEQEzoqF5wxg/RTx5yNvnK3oUPocbKUjSgJ1qj26vJU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kvkEFLPjN/qfoSjcIzyT35GhE0SgivRyAgIEAA=="}

看看你的屏幕截图,看起来你的字符串中有一个换行符,是这样吗?我在这里不知所措:http://prnt.sc/ddyq6y

关于javascript - Node JS,如何在 JSON 字符串中正确存储 Base64 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40906120/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com